Javascript 格式化javascript当前日期和时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6044150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Format javascript current date and time
提问by mheavers
Possible Duplicate:
How do I display a date/time in the user's locale format and time offset?
Hi - simple question - I just want to take this:
嗨 - 简单的问题 - 我只想接受这个:
document.getElementById("time").innerHTML= new Date();
and format it into something legible, like this:
并将其格式化为清晰易读的内容,如下所示:
May 18, 2011 7:45 AM
making sure it is localized to whomever might be seeing it. Currently, it prints out as this:
确保它被本地化给任何可能看到它的人。目前,它打印如下:
Wed May 18 2011 07:46:25 GMT-0400 (EDT)
How do I do this?
我该怎么做呢?
采纳答案by Jon Trauntvein
Look up the reference for
查找参考资料
Date.toLocaleString()
Date.toLocaleDateString(), and
Date.toLocaleTimeString().
回答by Gary Green
Steven Levithan's dateFormat()
(only 1.2KB when minified and gziped!) should do just what you need.
Steven Levithan 的dateFormat()
(缩小和压缩后只有 1.2KB!)应该可以满足您的需求。
Javascript
Javascript
// Formatting in: May 18, 2011 7:45 AM
var formattedDate = new Date().format('mmm dd, yyyy h:mm TT');
document.getElementById("time").innerHTML= formattedDate;