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
🌐
30 Seconds of Code
30secondsofcode.org › home › javascript › date › date to unix timestamp
Convert between a JavaScript Date object and a Unix timestamp - 30 seconds of code
January 7, 2024 - This means that you can convert between Date objects and Unix timestamps by dividing or multiplying by 1000. const toTimestamp = date => Math.floor(date.getTime() / 1000); const fromTimestamp = timestamp => new Date(timestamp * 1000); ...
Discussions

Add Unix Timestamp and Javascript Timestamp converstions to the date/time app - Feature Requests - n8n Community
I suggest adding two to easily convert Unix and Javascript timestamps. My use case: I’m exporting Zillow API data and all the listing date/times and change date/times are in Unix Time Stamp (aka milliseconds) Example: listingDateTimeOnZillow: 1699724220000. More on community.n8n.io
🌐 community.n8n.io
0
January 10, 2024
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
How to convert a future date into unix timestamp in JavaScript?
new Date(2020,12,12,12,30).getTime() would suffice ie. new Date(year,month,date,hour,min,sec,ms) More on reddit.com
🌐 r/learnjavascript
4
1
December 22, 2018
🌐
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.
🌐
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.
🌐
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
const myUnixTimestamp = 1691622800; // start with a Unix timestamp const myDate = new Date(myUnixTimestamp * 1000); // convert timestamp to milliseconds and construct Date object console.log(myDate); // will print "Thu Aug 10 2023 01:13:20" followed by the local timezone on browser console
🌐
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().
Find elsewhere
🌐
Epoch Converter
epochconverter.com
Epoch Converter - Unix Timestamp Converter
All examples return the epoch timestamp in seconds (and not milliseconds). The full list in the programming section also includes examples for converting human-readable dates to epoch time. ... Thanks to everyone who sent me corrections and updates! More date-related programming examples: What's the current week number? - What's the current day number? Please note: All tools on this page are based on the date & time settings of your computer and use JavaScript to convert times.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date
Date - JavaScript | MDN
A JavaScript date is fundamentally specified as the time in milliseconds that has elapsed since the epoch, which is defined as the midnight at the beginning of January 1, 1970, UTC (equivalent to the UNIX epoch). This timestamp is timezone-agnostic and uniquely defines an instant in history.
🌐
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 ...
🌐
Day.js
day.js.org › docs › en › display › unix-timestamp
Unix Timestamp · Day.js
This returns the Unix timestamp (the number of seconds since the Unix Epoch) of the Day.js object.
🌐
n8n
community.n8n.io › feature requests
Add Unix Timestamp and Javascript Timestamp converstions to the date/time app - Feature Requests - n8n Community
January 10, 2024 - The idea is: The Date and Time app has 7 actions. I suggest adding two to easily convert Unix and Javascript timestamps. My use case: I’m exporting Zillow API data and all the listing date/times and change date/times are in Unix Time Stamp (aka milliseconds) Example: listingDateTimeOnZillow: 1699724220000.
🌐
Medium
medium.com › @python-javascript-php-html-css › a-guide-to-obtaining-a-timestamp-in-javascript-04a81f4e5b9c
A Guide to Obtaining a Timestamp in JavaScript
September 22, 2024 - By default, a Unix timestamp is in UTC (Coordinated Universal Time), but often developers need to convert it to a local time zone. This can be achieved using the Intl.DateTimeFormat object, which provides a way to format dates and times according to a specific locale and time zone. For instance, you can use new Date() to create a date object from a timestamp and then format it using toLocaleString() with options for the desired time zone.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-convert-unix-timestamp-to-time-in-javascript
How to convert Unix timestamp to time in JavaScript ? - GeeksforGeeks
July 12, 2025 - The Intl.DateTimeFormat object allows for formatting dates and times according to locale-specific conventions. This method provides a flexible and powerful way to format the time extracted from a UNIX timestamp.
🌐
InfluxData
influxdata.com › home › how to get, convert & format javascript date from timestamp | influxdata
How to Get, Convert & Format JavaScript Date From Timestamp | InfluxData
February 10, 2023 - The Date.now() method is a built-in function in JavaScript that allows developers to retrieve the current timestamp quickly. Its primary advantage is that it returns the number of milliseconds, rather than seconds or minutes, since January 1, ...
🌐
Unix Time Stamp
unixtimestamp.com
Unix Timestamp
CSS Formatter GO Formatter HTML Beautifier & Formatter Javascript Formatter Javascript Obfuscate JSON Formatter & Beautifier JSON Editor JSON Validator Perl Formatter PHP Formatter Python Formatter Ruby Formatter SQL Formatter XML Formatter & Beautifier ... Supports Unix timestamps in seconds, milliseconds, microseconds and nanoseconds.
🌐
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 ...
🌐
npm
npmjs.com › package › unix-timestamp
unix-timestamp - npm
Tiny library to create and manipulate Unix timestamps in Javascript. (A Unix timestamp is the number of seconds elapsed since Unix epoch time, i.e.
      » npm install unix-timestamp
    
Published   Aug 11, 2024
Version   1.1.0
Author   Simon Goumaz
🌐
Day.js
day.js.org › docs › en › display › unix-timestamp-milliseconds
Unix Timestamp (milliseconds) · Day.js
This returns the number of milliseconds since the Unix Epoch of the Day.js object.
🌐
Dana Woodman
danawoodman.com › writing › javascript-date-unix-epoc-timestamp
How to convert a JavaScript date to a Unix timestamp (epoch)?
January 31, 2022 - To convert a JavaScript Date to a Unix timestamp you can use either the Date.prototype.getTime() or the Date.prototype.valueOf() method on the Date object, both of which return the number of milliseconds (ms) since the Unix Epoch, then divide it by 1000 to get the number of seconds since the ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Glossary › Unix_time
Unix time - Glossary | MDN
On the web platform, Unix time is used for timestamps, and is given as the number of milliseconds since the beginning of the Unix epoch.