Timestamp in milliseconds

To get the number of milliseconds since Unix epoch, call Date.now:

Date.now()

Alternatively, use the unary operator + to call Date.prototype.valueOf:

+ new Date()

Alternatively, call valueOf directly:

new Date().valueOf()

To support IE8 and earlier (see compatibility table), create a shim for Date.now:

if (!Date.now) {
    Date.now = function() { return new Date().getTime(); }
}

Alternatively, call getTime directly:

new Date().getTime()

Timestamp in seconds

To get the number of seconds since Unix epoch, i.e. Unix timestamp:

Math.floor(Date.now() / 1000)

Alternatively, using bitwise-or to floor is slightly faster, but also less readable and may break in the future (see explanations 1, 2):

Date.now() / 1000 | 0

Timestamp in milliseconds (higher resolution)

Use performance.now:

var isPerformanceSupported = (
    window.performance &&
    window.performance.now &&
    window.performance.timing &&
    window.performance.timing.navigationStart
);

var timeStampInMs = (
    isPerformanceSupported ?
    window.performance.now() +
    window.performance.timing.navigationStart :
    Date.now()
);

console.log(timeStampInMs, Date.now());

Answer from daveb on Stack Overflow
🌐
Coderwall
coderwall.com › p › rbfl6g › how-to-get-the-correct-unix-timestamp-from-any-date-in-javascript
How to get the correct Unix Timestamp from any Date in JavaScript (Example)
June 26, 2023 - Copy & paste the following code at the beginning of your JavaScript: Date.prototype.getUnixTime = function() { return this.getTime()/1000|0 }; if(!Date.now) Date.now = function() { return new Date(); } Date.time = function() { return ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date › now
Date.now() - JavaScript | MDN
The Date.now() static method returns the number of milliseconds elapsed since the epoch, which is defined as the midnight at the beginning of January 1, 1970, UTC.
Discussions

get unix timestamp of given date
There was an error while loading. Please reload this page · Is it current supported? In moment we have moment.valueOf() More on github.com
🌐 github.com
10
December 31, 2016
How to use JavaScript to Get Timestamps Explained with Examples - Guide - The freeCodeCamp Forum
You can use Date.now() to get the current timestamp in milliseconds. You can easily convert the timestamp to seconds like this: Math.floor(Date.now() / 1000) If your browser does not support Date.now() , you can use new Date().getTime() to get the timestamp in milliseconds. More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
2
June 26, 2019
How can I convert "2020-11-18-05:12" into unix timestamp? and then add 5 hours to it?
Prerequisite: dealing with dates and times in programming is one of the most non-trivial things you can ever undertake. Until the JS standard library can handle date-time in a sane manner, seriously consider using a library like Luxon instead of trying to wrangle time on your own! You should generally allow the browser to calculate the local time, instead of changing the timestamp. Let's break down why. First, to get the ms from epoch: const dateTimeString = '2020-11-18-05:12' const [years, months, days, hours, minutes, seconds = 0, ms = 0] = dateTimeString.split(/[-:]/).map(n => +n) const unixDate = new Date(years, months - 1, days, hours, minutes, seconds, ms).valueOf() If you take that number, add 18,000,000 to it (5hrs in ms), then pass that number back into the Date constructor, you will find that the time is now wrong by 5 hours. This is because the Date constructor assumes all dates are entered in UTC, and will automatically use the timezone of the local machine to display the appropriate time (assuming that is what you are trying to accomplish). Also assuming that all you want is to display that time string in the user's local time, just remove the .valueOf() and you have a timezone appropriate Date object. As an aside, that string is almost an ISO 8601-1:2019 compliant date-time string, 2020-11-18T05:12:00, which can be converted directly with the Date constructor. Edit: A better way to offset the time, is to use a date-time+timezone string: const dateTimeString = '2020-11-18-05:12' const offset = '+05:00' const [years, months, days, hours, minutes, seconds = '00', ms = '000'] = dateTimeString.split(/[-:]/) const date = new Date(`${years}-${months}-${days}T${hours}:${minutes}:${seconds}.${ms}${offset}`) More on reddit.com
🌐 r/learnjavascript
4
1
November 18, 2020
converting the Unix Timestamp with GTM
There are lots of articles online about converting a Unix timestamp into date format using JavaScript - see e.g. this: https://coderrocketfuel.com/article/convert-a-unix-timestamp-to-a-date-in-vanilla-javascript You can do all the code in a Custom JavaScript variable. Simo More on reddit.com
🌐 r/GoogleTagManager
2
1
September 29, 2020
🌐
Futurestud.io
futurestud.io › tutorials › how-to-get-a-unix-timestamp-in-javascript-or-node-js
How to Get a UNIX Timestamp in JavaScript or Node.js
June 23, 2022 - JavaScript’s global Date object comes with methods to interact with dates and times. The Date.now() function returns the current timestamp in milliseconds. These milliseconds are counted from the UNIX epoch.
🌐
Nesin
nesin.io › blog › javascript-date-to-unix-timestamp
How to convert Date to Unix Timestamp in Javascript
April 2, 2023 - In Javascript, Date object has getTime() method and it returns number of milliseconds since the Unix epoch which is January 1, 1970 00:00:00 UTC · And unix timestamp are mostly represented in seconds not milliseconds and so we'll be converting ...
🌐
Sentry
sentry.io › sentry answers › javascript › convert unix timestamp to date and time in javascript
Convert Unix timestamp to date and time in JavaScript | Sentry
To convert these into human-readable dates and times, we can use JavaScript’s Date object. This object’s constructor takes a value similar to a Unix timestamp, but in milliseconds rather than seconds.
🌐
Maslosoft
maslosoft.com › knowledge base › how to get unix timestamp from javascript date?
How to get UNIX timestamp from JavaScript Date?
April 6, 2023 - In JavaScript there is no direct method to obtain seconds based timestamp value, however it can be computed from Date.getTime() function, which return milliseconds instead of seconds.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-get-the-timestamp-in-javascript
How to Get the Timestamp in JavaScript? - GeeksforGeeks
July 23, 2025 - This approach leverages the unary plus operator (+) to implicitly convert the Date object into a number, effectively yielding the timestamp. Example: This example shows the use of +new Date() method to get the timestamp.
🌐
Educative
educative.io › answers › how-to-get-the-timestamp-in-javascript
How to get the timestamp in JavaScript
Line 3: We call the valueOf() method on the currentDate object to get the timestamp in milliseconds and store it in the variable timestamp. Line 4: We log the timestamp to the console, displaying the milliseconds since the Unix Epoch.
🌐
GitHub
github.com › date-fns › date-fns › issues › 372
get unix timestamp of given date · Issue #372 · date-fns/date-fns
December 31, 2016 - There was an error while loading. Please reload this page · Is it current supported? In moment we have moment.valueOf()
Author   ravins
🌐
JavaScript in Plain English
javascript.plainenglish.io › how-to-get-a-timestamp-in-javascript-63c05f19e544
How to Get a Timestamp in JavaScript? | by John Au-Yeung | JavaScript in Plain English
May 1, 2021 - We can use the + operator to convert the date object right to a UNIX timestamp. ... The + operator before the date object triggers the valueOf method in the Date object to return the timestamp as a number.
🌐
Fireship
fireship.dev › get-current-timestamp-javascript
How to get the current timestamp in JavaScript
The UNIX timestamp is defined as the number of seconds since January 1, 1970 UTC. In JavaScript, in order to get the current timestamp, you can use Date.now().
🌐
CoreUI
coreui.io › answers › how-to-get-current-timestamp-in-javascript
How to get current timestamp in JavaScript · CoreUI
September 30, 2025 - Use Date.now() to get the current timestamp in JavaScript - the fastest and most reliable method for milliseconds since Unix epoch.
🌐
JanBask Training
janbasktraining.com › community › java › how-do-i-get-a-timestamp-in-javascript
How do I get a timestamp in JavaScript? | JanBask Training Community
July 10, 2025 - This question explores how to retrieve the current time in JavaScript as a timestamp (milliseconds since epoch), and explains common methods like Date.now() and new Date().getTime().
🌐
freeCodeCamp
forum.freecodecamp.org › guide
How to use JavaScript to Get Timestamps Explained with Examples - Guide - The freeCodeCamp Forum
June 26, 2019 - You can use Date.now() to get the current timestamp in milliseconds. You can easily convert the timestamp to seconds like this: Math.floor(Date.now() / 1000) If your browser does not support Date.now() , you can use new Date().getTime() to get ...
🌐
Codemia
codemia.io › knowledge-hub › path › How-do-I-get-a-timestamp-in-JavaScript
How do I get a timestamp in JavaScript?
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-return-the-current-timestamp-with-momentjs
How to Return the Current Timestamp with Moment.js? - GeeksforGeeks
July 23, 2025 - JavaScript · // script.js const ... unixTimestamp); Output: Current Unix Timestamp: 1721910320 · In this approach, we are using the valueOf() method from Moment.js to get the current timestamp in milliseconds....
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date
Date - JavaScript | MDN
For example, the timestamp 0 represents a unique instant in history, but it can be interpreted in two ways: As a UTC time, it is midnight at the beginning of January 1, 1970, UTC, As a local time in New York (UTC-5), it is 19:00:00 on December 31, 1969. The getTimezoneOffset() method returns ...