如何从具有以下格式的日期创建 Date 对象:
03/23/2016 02:00:00 PM
最佳答案
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.
关于javascript - 如何使用 12 小时格式创建 JavaScript 日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36190572/