How to create a date object from string in javascr

2019-01-04 06:56发布

This question already has an answer here:

Having this string 30/11/2011. I want to convert it to date object.

Do I need to use :

Date d = new Date(2011,11,30);   /* months 1..12? */

or

Date d = new Date(2011,10,30);   /* months 0..11? */

?

8条回答
The star\"
2楼-- · 2019-01-04 07:35

First extract the string like this

var dateString = str.match(/^(\d{2})\/(\d{2})\/(\d{4})$/);

Then,

var d = new Date( dateString[3], dateString[2]-1, dateString[1] );
查看更多
祖国的老花朵
3楼-- · 2019-01-04 07:37
var d = new Date(2011,10,30);

as months are indexed from 0 in js.

查看更多
登录 后发表回答