Parse Date String to Date in JavaScript

后端 未结 3 1912
挽巷
挽巷 2021-01-27 03:57

I have this date string \"2013:05:12 11:41:31\"

I tried:

var strDate = \"2013:05:12 11:41:31\";
var dateParts = strDate.split(\":\");

var date = new Da         


        
3条回答
  •  悲哀的现实
    2021-01-27 04:24

    var strDate = "2013:05:12 11:41:31";
    
    /*
    You can parse a string, but it has to be a single string argument.
    
    An array of arguments needs to be numbers, which are easy to coerce.
    avoid leading 0s- 08 and 09 can cause octal problems for some users.
    
    */
    
    var ymd = strDate.match(/[1-9]\d*/g);
    var day= new Date(+ymd[0],ymd[1]-1,+ymd[2]);
    day.toLocaleDateString()
    

    // returned (String) value: Sunday, May 12, 2013

    Without a timezone you don't have a particular date, just May 12 wherever you are.

    Javascript defaults to the users local settings, but some applications default to GMT.

提交回复
热议问题