Converting a string formatted YYYYMMDDHHMMSS into a JavaScript Date object

后端 未结 6 654
借酒劲吻你
借酒劲吻你 2021-01-06 02:33

I have a string with a date in it formatted like so: YYYYMMDDHHMMSS. I was wondering how I would convert it into a JavaScript Date object with JavaScript.

Thanks in

6条回答
  •  时光取名叫无心
    2021-01-06 03:03

    There is no time zone information in your string. It most likely is GMT, so that should be accounted for when you make the Date. You can easily do the conversion with simple javascript methods.

    Date.Brit= (function(){
        return Date.parse('2/6/2009')> Date.parse('6/2/2009');
    })()
    
    
    var s= "20091202093000"
    var D= s.match(/(\d{2})/g);
    if(Date.Brit){
        D.splice(2, 2, D[3],D[2]);
    }
    var day= new Date(Date.parse(D[2]+'/'+D[3]+'/'+
    D[0]+D[1]+' '+D.splice(4).join(':')+' GMT'));
    
    day.toUTCString()+'\n'+day
    
    
    /*  returned value: (String)
    Wed, 02 Dec 2009 09:30:00 GMT
    Wed Dec 02 2009 04:30:00 GMT-0500 (Eastern Standard Time)
    */
    

提交回复
热议问题