getTime()
给出了自1970年1月1日以来的毫秒数。
我如何获得自2012年1月1日以来的毫秒数?
这就是我目前所拥有的:
var n = new Date()
var a = n.getTime()
console.log (a)
答案 0 :(得分:4)
怎么样:
var diff = new Date() - new Date(2012, 0, 1); // diff is in milliseconds
用于计算差异,包括当地时区偏差,或
var diff = new Date() - Date.UTC(2012, 0, 1); // diff in ms
了解更多科学解决方案。
请注意,Javascript中的月份为零。
答案 1 :(得分:3)
var ms = +new Date() - new Date( '2012/01/01' )
或根据需要制作第二个约会对象new Date( '2012/01/01 GMT' )
答案 2 :(得分:2)
以下是一个例子:
示例输出。请注意DurationGMT
& DurationLocal
是不同的。与日期进行比较总是使用GMT。
Now: 1,326,054,979,124 ms (Sun, 08 Jan 2012 20:36:19 GMT)
Start1: 1,325,376,000,000 ms (Sun, 01 Jan 2012 00:00:00 GMT)
Start2: 1,325,376,000,000 ms (Sun, 01 Jan 2012 00:00:00 GMT)
Start3: 1,325,376,000,000 ms (Sun, 01 Jan 2012 00:00:00 GMT)
DurationGMT: 678,979,124 ms (Accurate method)
StartLocal1: 1,325,397,600,000 ms (Sun, 01 Jan 2012 06:00:00 GMT)
DurationLocal: 657,379,124 ms !!! Don't use this method
以下是获得GMT日期的三种方法,#3将是您想要的。
var now = new Date();
var startOfYear1 = createGMTDate1(2012, 0, 1, 0, 0, 0, 0);
var startOfYear2 = createGMTDate2(2012, 0, 1, 0, 0, 0, 0);
var startOfYear3 = createGMTDate3(2012, 0, 1, 0, 0, 0, 0);
var durationGMTMillis = now.getTime() - startOfYear1.getTime(); // accurate
var startOfYearLocal1 = new Date(2012, 0, 1, 0, 0, 0, 0);
var durationLocalMillis = now.getTime() - startOfYearLocal1.getTime(); // inaccurate
function createGMTDate1(year, month, date, hours, mins, secs, millis) {
var dateDefaultTz = new Date(year, month, date, hours, mins, secs, millis);
var localeTzGMTMillis = dateDefaultTz.getTime();
var localeTzGMTOffsetMillis = dateDefaultTz.getTimezoneOffset() * 60 * 1000;
var dateGMT = new Date(localeTzGMTMillis - localeTzGMTOffsetMillis);
return dateGMT;
}
function createGMTDate2(year, month, date, hours, mins, secs, millis) {
var dateGMT = new Date(0);
dateGMT.setUTCFullYear(year);
dateGMT.setUTCMonth(month);
dateGMT.setUTCDate(date);
dateGMT.setUTCHours(hours);
dateGMT.setUTCMinutes(mins);
dateGMT.setUTCSeconds(secs);
dateGMT.setUTCMilliseconds(millis);
return dateGMT;
}
function createGMTDate3(year, month, date, hours, mins, secs, millis) {
var dateGMT = new Date(Date.UTC(year, month, date, hours, mins, secs, millis));
return dateGMT;
}
答案 3 :(得分:0)
正如其他人所说,解决方案是减去Date
个实例:
var ms = now - before;
这是有效的,因为-
运算符将其操作数转换为Number
(ECMAScript Language Specification, Edition 5.1,第11.6.2节)。相应的ToNumber
算法检查对象是否具有valueOf
方法,如果存在则调用它(第9.3,9.1和8.12.8节)。
现在,Date.prototype.valueOf()
实例继承的Date
方法为给定的Date.prototype.getTime()
实例返回与Date
相同的值(第15.9.5.8节)。这是自1970年1月1日(CE)00:00:00.000 UTC(“epoch”)以来的毫秒数(第15.9.1.1节)。
如果要与当地时间进行比较,第一个操作数是显而易见的:
var now = new Date();
第二部分有点棘手,因为你想从2012年1月1日(CE)00:00:00.000 GMT 算起。为此你不能使用
var before = new Date(2012, 0, 1);
(或其变体),因为它使用00:00:00.000 本地时间(第15.9.3.1节)。至少有两种方法可以完成这项工作:
使用日期格式的字符串值,该格式必须由符合ECMAScript Edition 5.1的实现(第15.9.1.15节)识别:
var ms = new Date() - new Date("2012-01-01T00:00:00.000Z");
如果您担心向后兼容性,可以明确设置时间(第15.9.5节):
var before = new Date();
before.setUTCFullYear(2012, 0, 1);
before.setUTCHours(0, 0, 0, 0);
var ms = now - before;