Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript method to find number of milliseconds from 1970-01-01? [duplicate]

Tags:

Say, I have a date

var dt = new Date('2012-01-01');

Is there a method in Javascript to return number of milliseconds since 1970-01-01? For this particulat date it would return 1325376000000

For this purpose, there is a method "toUTC()" that runs in Chrome only. I know this because i can do that in Chrome's console. See screen attached below:

enter image description here

However, When I search about this method on the internet, I don't find it and it doesn't work in Firefox either which is very wierd and I am very confused.

Anyhow, if you know any way to get this, will be really appreciated.

Thank You

like image 296
Cute_Ninja Avatar asked Jun 26 '14 16:06

Cute_Ninja


People also ask

Which Date method returns the number of milliseconds since January 1 1970?

JavaScript Date getTime() getTime() returns the number of milliseconds since January 1, 1970 00:00:00.

Which method returns the number of milliseconds elapsed since January 1 1970 00 00 00 utc?

now() The static Date. now() method returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.

How to get milliseconds from Date in JavaScript?

JavaScript - Date getMilliseconds() Method Javascript date getMilliseconds() method returns the milliseconds in the specified date according to local time. The value returned by getMilliseconds() is a number between 0 and 999.

Which method returns the number of milliseconds?

getTime() The getTime() method returns the number of milliseconds since the ECMAScript epoch. You can use this method to help assign a date and time to another Date object. This method is functionally equivalent to the valueOf() method.


2 Answers

Use the getTime function:

var dt = new Date('2012-01-01');
var time = dt.getTime();
like image 79
Cameron Tinker Avatar answered Sep 19 '22 13:09

Cameron Tinker


You can get it with:

new Date('2012-01-01').getTime();

The getTime() method returns the number of milliseconds between midnight of January 1, 1970 and the specified date.

like image 26
gipinani Avatar answered Sep 21 '22 13:09

gipinani