The UNIX timestamp is defined as the total number of seconds passed since January 1st, 1970 00:00:00 UTC.
To get the current timestamp in JavaScript, you can call the now()
method on the Date
object without passing any parameters to it.
The Date.now()
method returns the timestamp in milliseconds.
For example,
// Returns the timestamp in milliseconds const timeStamp = Date.now(); console.log(timeStamp); // Output: 👉 1679208370936
Please note that the timestamp returned by the Date.now()
method is in milliseconds, not in seconds.
So, if you want to convert it to seconds, you have to divide it by 1000.
For example,
// Returns the timestamp in milliseconds const timeStamp = Date.now(); // Convert timestamp to seconds const timeStampInSeconds = Math.floor(timeStamp/1000); console.log(timeStampInSeconds); // Output: 👉 1679208602
Use the getTime() and valueOf() Methods to Get the Current Timestamp
There are two more methods in JavaScript that you can use to get the current timestamp, getTime()
and valueOf()
.
The getTime()
and valueOf()
methods also returns the current timestamp in milliseconds.
For example,
// Get the timestamp in milliseconds const timeStamp = new Date().getTime(); console.log(timeStamp); // Output: 👉 1679209323119
The same way you can call the valueOf()
method on the Date
object to get the timestamp in milliseconds.
For example,
// Get the timestamp in milliseconds const timeStamp = new Date().valueOf(); console.log(timeStamp); // Output: 👉 1679209516375
Please note that the value of the timestamp will be different in your case as it is increasing every second.
Converting the Timestamp to a Date Object
If you already have the value of the timestamp and you want to convert it to a human readable date, you can simply pass it to the Date()
constructor as an argument.
The Date()
constructor will return the date object in a human readable format.
For example,
// Timestamp const timestamp = 1679209516375; // Convert timestamp to date object const date = new Date(timestamp); console.log(date); // Output: 👉 Sun Mar 19 2023 12:35:16 GMT+0530 (India Standard Time)
Additionally, if you want to get the year, month and day from the date object, you can call the getFullYear()
, getMonth()
and getDate()
methods respectively.
For example,
// Timestamp const timestamp = 1679209516375; // Convert timestamp to date object const date = new Date(timestamp); console.log(date); // Output: 👉 Sun Mar 19 2023 12:35:16 GMT+0530 (India Standard Time) const year = date.getFullYear(); // Output: 👉 2023 const month = date.getMonth() + 1; // Output: 👉 3 const day = date.getDate(); // Output: 👉 19
Please note that the getMonth()
method returns the month index(0 based), therefore, we add +1 to get the month number.
Converting a Date String to Timestamp
If you have a date string and you want to get the timestamp from it, you can first pass the date string to the Date()
constructor and then call the getTime()
method on the returned date object to get the timestamp from it.
For example,
// Fixed date string const date = new Date('Sun Mar 19 2023 12:35:16'); // Get the timestamp from the date object const timestamp = date.getTime(); console.log(timestamp); // Output: 👉 1679209516000
Ofcourse, you can pass the date strings in other formats also, the Date()
constructor takes care of it.
That’s all for this article. Thanks for reading!