简体   繁体   English

使用AM / PM将日期字符串转换为javascript日期对象

[英]Convert date string with AM/PM to javascript date object

I would like to convert 'April 25, 2016 Mon 09:59 PM' to a javascript date object so that I can do some basic math on it. 我想将“ 2016年4月25日星期一09:59 PM”转换为javascript日期对象,以便可以对其进行一些基本的数学运算。 However, Date.parse() does not work producing Nan. 但是,Date.parse()不能用于生成Nan。 I assume it's because Date.parse has specific requirements for formatting. 我认为这是因为Date.parse对格式有特定的要求。

I cannot use any external libraries besides jQuery. 除了jQuery,我不能使用任何外部库。

try to remove the date of the week using regex: 尝试使用正则表达式删除星期几:

 var date = 'April 25, 2016 Mon 09:59 PM'; date = new Date(date.replace(/([0-9]{4}) .{3}/, '$1')); document.body.innerHTML = date; 

The correct format would be: 正确的格式为:

var myDate=new Date('April 25, 2016 09:59 PM')

or 要么

var myDate=new Date('Mon April 25, 2016 09:59 PM')

now you have a myDate object of type Date and you may access all the methods for the date object to do your calculations. 现在,您具有日期类型的myDate对象,并且可以访问该日期对象的所有方法来进行计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM