Parse 'Date & Time' string in Javascript which are of custom format

前端 未结 5 1151
-上瘾入骨i
-上瘾入骨i 2020-11-30 10:08

I have to parse a date and time string of format \"2015-01-16 22:15:00\". I want to parse this into JavaScript Date Object. Any help on this?

I tried some jquery plu

5条回答
  •  长情又很酷
    2020-11-30 10:44

    If you are sure it's in the desired format and don't need to error check, you can parse it manually using split (and optionally replace). I needed to do something similar in my project (MM/DD/YYYY HH:mm:ss:sss) and modified my solution to fit your format. Notice the subtraction of 1 in the month.

    var str = "2015-01-16 22:15:00"; 
    //Replace dashes and spaces with : and then split on :
    var strDate = str.replace(/-/g,":").replace(/ /g,":").split(":");
    var aDate = new Date(strDate[0], strDate[1]-1, strDate[2], strDate[3], strDate[4], strDate[5]) ; 
    

提交回复
热议问题