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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:00:03  来源:igfitidea点击:

Format javascript current date and time

javascript

提问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;