如何从 12 小时格式创建 javascript 日期
How to create a javascript date from 12 hour format
如何从具有以下格式的日期创建日期对象:
03/23/2016 02:00:00 PM
var date = new Date("3/23/2016 02:00:00 PM");
console.log(date);
然后您可以访问 Date 对象的所有方法。
Date
对象可以解析字符串:new Date('03/23/2016 02:00:00 PM')
例如:
var date = new Date('03/23/2016 02:00:00 PM') // => "Wed Mar 23 2016 14:00:00 GMT-0700 (PDT)"
date.getFullYear() // => 2016
但是,我建议使用其他人已经花时间考虑边缘情况(如时区等)的库(我用过的一个很好的库是 moment.js)。
请记住(来自 moment.js 文档):
Warning: Browser support for parsing strings is inconsistent. Because there is no specification on which formats should be supported, what works in some browsers will not work in other browsers.
For consistent results parsing anything other than ISO 8601 strings, you should use String + Format.
正在查看MDN
var date = new Date('03/23/2016 02:00:00 PM')
您可以使用类似 date.js 的内容:
先用脚本,再写日期:
<script type="text/javascript" src="http://www.datejs.com/build/date.js"></script>
....
document.write(new Date().toString("dd:MM:yyyy hh:mm:ss tt"));
如何从具有以下格式的日期创建日期对象:
03/23/2016 02:00:00 PM
var date = new Date("3/23/2016 02:00:00 PM");
console.log(date);
然后您可以访问 Date 对象的所有方法。
Date
对象可以解析字符串:new Date('03/23/2016 02:00:00 PM')
例如:
var date = new Date('03/23/2016 02:00:00 PM') // => "Wed Mar 23 2016 14:00:00 GMT-0700 (PDT)"
date.getFullYear() // => 2016
但是,我建议使用其他人已经花时间考虑边缘情况(如时区等)的库(我用过的一个很好的库是 moment.js)。
请记住(来自 moment.js 文档):
Warning: Browser support for parsing strings is inconsistent. Because there is no specification on which formats should be supported, what works in some browsers will not work in other browsers.
For consistent results parsing anything other than ISO 8601 strings, you should use String + Format.
正在查看MDN var date = new Date('03/23/2016 02:00:00 PM')
您可以使用类似 date.js 的内容: 先用脚本,再写日期:
<script type="text/javascript" src="http://www.datejs.com/build/date.js"></script>
....
document.write(new Date().toString("dd:MM:yyyy hh:mm:ss tt"));