How can I convert string to datetime with format specification in JavaScript?

前端 未结 15 1025
终归单人心
终归单人心 2020-11-22 02:35

How can I convert a string to a date time object in javascript by specifying a format string?

I am looking for something like:

var dateTime = convert         


        
15条回答
  •  借酒劲吻你
    2020-11-22 03:20

    var temp1 = "";
    var temp2 = "";
    
    var str1 = fd; 
    var str2 = td;
    
    var dt1  = str1.substring(0,2);
    var dt2  = str2.substring(0,2);
    
    var mon1 = str1.substring(3,5);
    var mon2 = str2.substring(3,5);
    
    var yr1  = str1.substring(6,10);  
    var yr2  = str2.substring(6,10); 
    
    temp1 = mon1 + "/" + dt1 + "/" + yr1;
    temp2 = mon2 + "/" + dt2 + "/" + yr2;
    
    var cfd = Date.parse(temp1);
    var ctd = Date.parse(temp2);
    
    var date1 = new Date(cfd); 
    var date2 = new Date(ctd);
    
    if(date1 > date2) { 
        alert("FROM DATE SHOULD BE MORE THAN TO DATE");
    }
    

提交回复
热议问题