我注意到JavaScript的new Date()功能在接受多种格式的日期时非常聪明.
Xmas95 = new Date("25 Dec, 1995 23:15:00")
Xmas95 = new Date("2009 06 12,12:52:39")
Xmas95 = new Date("20 09 2006,12:52:39")
Run Code Online (Sandbox Code Playgroud)
在调用new Date()函数时,我无法在任何地方找到显示所有有效字符串格式的文档.
这用于将字符串转换为日期.如果我们看一下相反的方面,即将日期对象转换为字符串,直到现在我的印象是JavaScript没有内置的API来将日期对象格式化为字符串.
编者注:以下方法是提问者的企图,关于特定浏览器的工作,但也不会在一般的工作; 请参阅此页面上的答案以查看一些实际解决方案.
今天,我在toString()日期对象上使用了该方法,并且令人惊讶的是它用于将日期格式化为字符串.
var d1 = new Date();
d1.toString('yyyy-MM-dd'); //Returns "2009-06-29" in Internet Explorer, but not Firefox or Chrome
d1.toString('dddd, MMMM ,yyyy') //Returns "Monday, June 29,2009" in Internet Explorer, but not Firefox or Chrome
Run Code Online (Sandbox Code Playgroud)
在这里,我找不到任何关于我们可以将日期对象格式化为字符串的方法的文档.
列出Date()对象支持的格式说明符的文档在哪里?
我有一个脚本,用JavaScript打印当前的日期和时间,但DATE总是错的.这是代码:
var currentdate = new Date();
var datetime = "Last Sync: " + currentdate.getDay() + "/" + currentdate.getMonth()
+ "/" + currentdate.getFullYear() + " @ "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":" + currentdate.getSeconds();
Run Code Online (Sandbox Code Playgroud)
它应该打印18/04/2012 15:07:33和打印3/3/2012 15:07:33
有帮助吗?谢谢