- Published on
How to convert Date to Unix Timestamp in Javascript
- Authors
- Name
- Ashik Nesin
- @AshikNesin
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 milliseconds to seconds by dividing it by 1000.
const date = new Date(); // Or any Date('YYYY-MM-DD')
const unixTimestamp = Math.floor(date.getTime() / 1000);
Happy converting date!