Javascript以特定格式写入时间

标签 javascript parsing datetime datejs

我正在使用 Javascript 和 Date.js,并且需要翻译时间的字符串表示形式。

我从这种格式的时间开始:

3:00am
2:30pm 
11:00am

...and need to put them in this format:

03:00:00
14:00:00
11:00:00

I was thinking of starting with Date.parse() and then printing out the hours, minutes, and seconds. But was wondering if there is a more elegant way to do this such as something along the lines of Date.format("HH:MM:SS")

What is a good way to do this?

Thanks!

Edit::::

It looks like you can use the format specifiers with Date.js: http://code.google.com/p/datejs/wiki/FormatSpecifiers

Date.parse("3:00am").toString("HH:mm:ss");

最佳答案

The answer to all your questions

<script type="text/javascript">
<!--

var d = new Date();
var curr_hour = d.getHours();
var curr_min = d.getMinutes();
var curr_sec = d.getSeconds();

document.write(curr_hour + ":" + curr_min + ":" + curr_sec);

//-->
</script>

关于Javascript以特定格式写入时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6657855/

相关文章:

javascript - 当主 url 没有改变但表格内容改变时解析网页

datetime - Flutter:DateTime.now 不反射(reflect)手动设置的时区

javascript - Minimax Alpha Beta Pruning Algorithm 解决 Tic Tac Toe(10x10 棋盘)需要花费太多时间

javascript - angular js日期输入格式

PHP数组到Json树格式

javascript - 以毫秒为单位的 d3.js 时间解析不起作用?

javascript - RESTful GET 资源应如何接受日期时间参数?

javascript - HTML 字符串到元素 mootools

local-storage - 在本地存储中保存 Javascript 模块对象

javascript - Google map API 页面未加载 - 解析 XML 错误