timestamp in JavaScript:
The Date.now() method returns the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC.
Syntax
[pastacode lang=”javascript” manual=”var%20timeInMs%20%3D%20Date.now()%3B%0A%09%09var%20timeStamp%20%3D%20Math.floor(Date.now()%20%2F%201000)%3B%0A” message=”jQuery Code” highlight=”” provider=”manual”/] [ad type=”banner”]Get a UNIX timestamp with Javascript
[pastacode lang=”javascript” manual=”var%20ts%20%3D%20Math.round((new%20Date()).getTime()%20%2F%201000)%3B%0A” message=”jQuery Code” highlight=”” provider=”manual”/]Return value
A Number representing the milliseconds elapsed since the UNIX epoch.
Description
Because now() is a static method of Date, we always use it as Date.now().
Usage
Date.getUnixTime() returns the Unix epoch time.
Date.now() polyfill for older browsers.
Date.time() is a a C-style helper function that returns the current Unix time.
Example1:
Date.getUnixTime() returns the Unix epoch time.
We frequently need to calculate with unix timestamp. There are several ways to grab the timestamp. For current unix timestamp easiest and fastest way is
[pastacode lang=”javascript” manual=”const%20dateTime%20%3D%20Date.now()%3B%0Aconst%20timestamp%20%3D%20Math.floor(dateTime%20%2F%201000)%3B%0A” message=”jQuery Code” highlight=”” provider=”manual”/]or
[pastacode lang=”javascript” manual=”const%20dateTime%20%3D%20new%20Date().getTime()%3B%0Aconst%20timestamp%20%3D%20Math.floor(dateTime%20%2F%201000)%3B%0A” message=”jQuery Code” highlight=”” provider=”manual”/]To get unix timestamp of a specific date pass YYYY-MM-DD or YYYY-MM-DDT00:00:00Z as parameter of Date constructor.
[pastacode lang=”javascript” manual=”const%20dateTime%20%3D%20new%20Date(‘2012-06-08’).getTime()%3B%0Aconst%20timestamp%20%3D%20Math.floor(dateTime%20%2F%201000)%3B%0A” message=”jQuery Code” highlight=”” provider=”manual”/]we can just add a + sign also when declaring a Date object like below
[pastacode lang=”javascript” manual=”const%20dateTime%20%3D%20%2Bnew%20Date()%3B%0Aconst%20timestamp%20%3D%20Math.floor(dateTime%20%2F%201000)%3B” message=”jQuery Code” highlight=”” provider=”manual”/][ad type=”banner”]
or for specific date
[pastacode lang=”javascript” manual=”const%20dateTime%20%3D%20%2Bnew%20Date(‘2012-06-08’)%3B%0Aconst%20timestamp%20%3D%20Math.floor(dateTime%20%2F%201000)%3B%0A” message=”jQuery Code” highlight=”” provider=”manual”/]Example2:
Date.now() polyfill for older browsers.
This method was standardized in ECMA-262 5th edition.
Engines which have not been updated to support this method can work around the absence of this method using the following shim:
Solution
To copy & paste the following code at the beginning of your JavaScript:
[pastacode lang=”javascript” manual=”Date.prototype.getUnixTime%20%3D%20function()%20%7B%20return%20this.getTime()%2F1000%7C0%20%7D%3B%0Aif(!Date.now)%20Date.now%20%3D%20function()%20%7B%20return%20new%20Date()%3B%20%7D%0ADate.time%20%3D%20function()%20%7B%20return%20Date.now().getUnixTime()%3B%20%7D%0A” message=”jQuery Code” highlight=”” provider=”manual”/][ad type=”banner”]