JavaScript won't parse GMT Date/Time Format

后端 未结 6 2073
我在风中等你
我在风中等你 2021-01-22 06:33

I\'m trying to get JavaScript to parse a date and time format for me, with the eventual aim of telling me the days passed since that date and the time right now (locally).

6条回答
  •  情歌与酒
    2021-01-22 07:23

    This should do it

    function dateFromUTC( dateAsString, ymdDelimiter )
    {
      var pattern = new RegExp( "(\\d{4})" + ymdDelimiter + "(\\d{2})" + ymdDelimiter + "(\\d{2}) (\\d{2}):(\\d{2}):(\\d{2})" );
      var parts = dateAsString.match( pattern );
    
      return new Date( Date.UTC(
          parseInt( parts[1] )
        , parseInt( parts[2], 10 ) - 1
        , parseInt( parts[3], 10 )
        , parseInt( parts[4], 10 )
        , parseInt( parts[5], 10 )
        , parseInt( parts[6], 10 )
        , 0
      ));
    }
    
    alert( dateFromUTC( "2008-10-01 06:21:43", '-' ) );
    

提交回复
热议问题