getTime()
gives you milliseconds since January 1, 1970.
How can I get the milliseconds since January 1, 2012?
This is what I currently have:
var n = new Date()
var a = n.getTime()
console.log (a)
getTime()
gives you milliseconds since January 1, 1970.
How can I get the milliseconds since January 1, 2012?
This is what I currently have:
var n = new Date()
var a = n.getTime()
console.log (a)
How about:
var diff = new Date() - new Date(2012, 0, 1); // diff is in milliseconds
for calculating differences including local time zone deviations, or
var diff = new Date() - Date.UTC(2012, 0, 1); // diff in ms
for more scientific solutions.
Note that months in Javascript are zero based.
var ms = +new Date() - new Date( '2012/01/01' )
or make that 2nd date object new Date( '2012/01/01 GMT' )
if desired
Here is an example:
Sample output. Notice how the DurationGMT
& DurationLocal
are different. When doing comparisons with dates always use 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
Here are three methods to get a GMT date, #3 would be what you want.
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;
}
As others have said, the solution is subtracting Date
instances:
var ms = now - before;
This works because the -
operator converts its operands to Number
(ECMAScript Language Specification, Edition 5.1, section 11.6.2). The corresponding ToNumber
algorithm checks if the object has a valueOf
method, and calls it if it exists (sections 9.3, 9.1, and 8.12.8).
It now happens that the Date.prototype.valueOf()
method, that Date
instances inherit, returns the same value as Date.prototype.getTime()
for a given Date
instance (section 15.9.5.8). Which is the number of milliseconds since January 1, 1970 (CE) 00:00:00.000 UTC ("epoch") (section 15.9.1.1).
The first operand is obvious if you want to compare against the local time:
var now = new Date();
The second part is a bit more tricky because you want to count from January 1, 2012 (CE) 00:00:00.000 GMT. For that you cannot use
var before = new Date(2012, 0, 1);
(or variations thereof) because it uses 00:00:00.000 local time (section 15.9.3.1). There are at least two ways to make this work:
Use a string value in a date format that must be recognized by conforming implementations of ECMAScript Edition 5.1 (section 15.9.1.15):
var ms = new Date() - new Date("2012-01-01T00:00:00.000Z");
If you are concerned about backwards compatibility, you can set the time explicitly (section 15.9.5):
var before = new Date();
before.setUTCFullYear(2012, 0, 1);
before.setUTCHours(0, 0, 0, 0);
var ms = now - before;