javascript - 使用 Javascript 格式化当前时间

标签 javascript date time format

我想用 javascript 获取特定格式的当前时间。

使用下面的函数并调用它会给我 2013 年 2 月 1 日星期五 13:56:40 GMT+1300(新西兰夏令时) 但我想格式化它 周五下午 2:00 2013 年 2 月 1 日

var d = new Date();
var x = document.getElementById("time");
x.innerHTML = d;

当然,上面的代码没有任何格式化逻辑,但我还没有遇到任何“有效”的格式化程序。

最佳答案

你可能想试试

var d = new Date();
d.toLocaleString();       // -> "2/1/2013 7:37:08 AM"
d.toLocaleDateString();   // -> "2/1/2013"
d.toLocaleTimeString();  // -> "7:38:05 AM"

Documentation

关于javascript - 使用 Javascript 格式化当前时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14638018/

相关文章:

javascript - 使用javascript在单个页面上减少具有相同类的多个数字

javascript - 为什么我的非常简单的 jQuery 不能工作?

MySQL:对于每个id,使用顺序日期来计算它们之间的空间

c++ - std::chrono 有多准确?

php - SQL/PHP : get all results within time X to Y, 检测中间是否有可用时间

c - 在 c 中设置自定义日期的问题

javascript - 带有文本区域的 html、css、javascript

java - 比较两个时间戳天数

excel - 将列拆分为日期和时间

javascript - 日期代码在 JS 中不起作用