解析日期对象字符串并以毫秒为单位获取时间

时间:2015-10-01 17:52:03

标签: javascript jquery

我在我的mongodb中存储new Date()

我试图查看日期是否少于5分钟,所以我尝试了:

// not working, maybe because the "previousTime" date object is now a string instead.
var isRecent = new Date().getTime() - previousTime.getTime() < 300000

// not working either, parseDate(previousTime).getTime() always gives the same 1443646800000 value
var isRecent = new Date().getTime() - parseDate(previousTime).getTime() < 3000;

function parseDate (input) {
  var parts = input.match(/(\d+)/g);
  // new Date(year, month [, date [, hours[, minutes[, seconds[, ms]]]]])
  return new Date(parts[0], parts[1]-1, parts[2]); // months are 0-based
}

1 个答案:

答案 0 :(得分:0)

将日期字符串放在Date.parse()中,获取自纪元以来的毫秒时间。

例如:

var dateAsString = new Date().toString();
var dateInMSSinceEpoch = Date.parse(dateAsString);