convert string into datetime format in Javascript

微笑、不失礼 提交于 2020-03-22 07:54:15

问题


i have a string which, i want to compare with a javascript datetime object. how to convert string "1/1/1912" into datetime using JavaScript so that i can compare like

if (EDateTime > ('1/1/1912'))  {...}

回答1:


You could do this simply with a split if you can guarantee the date format.

var dateArray = '1/1/1912'.split("/");
new Date(dateArray[2], dateArray[1], dateArray[0]);



回答2:


        var dateArray = '2012-02-17 01:10:59'.split(' ');
        var year = dateArray[0].split('-');
        var time = dateArray[1].split(':');

        var finishDate = new Date(year[0], year[1], year[2], time[0], time[1], time[2])



回答3:


How about using DateJS library?

It can convert 1/1/1912 to Monday, January 01, 1912 12:00:00 AM easily




回答4:


Convert your string to timestamp with Date object.

I found something like:

function toTimestamp(year,month,day,hour,minute,second){
   var datum = new Date(Date.UTC(year,month-1,day,hour,minute,second));
   return datum.getTime()/1000;
}

Year, month and day parts get with regular expressions.




回答5:


This library may be helpful.



来源:https://stackoverflow.com/questions/2161615/convert-string-into-datetime-format-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!