Javascript Datestring Converter with Timezone

时间:2012-09-13 12:49:59

标签: javascript datetime

使用JavaScript,我如何翻译这样的日期字符串:

Sep 13, 1:13PM GMT+01:00
Sep 12, 4:00PM EDT

要么是自纪元以来的Unix时间戳还是JavaScript毫秒?

我认为格式是:

MMM DD, h:mmtt ?K

1 个答案:

答案 0 :(得分:0)

我已经走到了这一步,接近答案。

  • 它对字符串执行正则表达式并将每个元素捕获到一个组
  • 时间的24小时表示已更正
  • 我认为这是当年
  • 未能考虑到时区
  • 使用parse方法
function getTimestamp(str){
 var re = /(\w{3}) (\d{2}), (\d):(\d{2})(\w{2}) (.*)/;
  var match = re.exec(str);
  if (match !== null) {
      var HH = match[5]==='PM' ? +match[3]+12 : match[3];
      return Date.parse(match[1]+' '+match[2]+', '+new Date().getFullYear()+' '+ HH +':'+match[4]);
  }
}

getTimestamp('Sep 12, 4:00PM EDT'); //=> 1347462000000