简体   繁体   English

将日期字符串转换为日期对象Javascript

[英]Convert Date String to Date Object Javascript

How to convert this date string 19/04/2015:21:43:47.40 to Date object. 如何将此日期字符串19/04/2015:21:43:47.40Date对象。 new Date('19/04/2015:21:43:47.40') returns invalid date. new Date('19/04/2015:21:43:47.40')返回无效日期。

To be absolutely sure I would split the string on any characters that aren't digits with a regex \\D+ . 绝对可以确保使用正则表达式\\D+将字符串拆分为非数字的任何字符。 Then you have an array with all the parts and you can pass it into new Date() in the correct order: 然后,您将获得一个包含所有部分的数组,并且可以按正确的顺序将其传递到new Date()中:

 var aParts = '19/04/2015:21:43:47.40'.split(/\\D+/); document.write(new Date(aParts[2], parseInt(aParts[1], 10)-1, aParts[0], aParts[3], aParts[4], aParts[5], aParts[6])); 

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

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