Borislav Hadzhiev
Wed Jan 19 2022·3 min read
Photo by Alex Knight
To convert a date string to a timestamp:
Date()
constructor.getTime()
method on the Date
object.getTime
method returns the number of milliseconds since the Unix Epoch.// ✅ with Date formatted as YYYY-MM-DD const dateStr1 = '2022-06-24'; const date1 = new Date(dateStr1); const timestamp = date1.getTime(); console.log(timestamp); // 👉️ 1656028800000 // 👇️ If you need to convert milliseconds to seconds // divide by 1000 const inSeconds = Math.floor(date1.getTime() / 1000); // ✅ with Date in other format, e.g. MM/DD/YYYY const dateStr = '06/24/2022'; const [month, day, year] = dateStr.split('/'); const date2 = new Date(+year, month - 1, +day); console.log(date2);
In the first example, we had a date string formatted as YYYY-MM-DD
, so we were
able to directly pass the date string as a parameter to the
Date()
constructor and get a Date
object back.
To get a timestamp, we called the
getTime
method on the Date
object.
The getTime
method returns a number that represents the milliseconds elapsed
between the 1st of January, 1970 and the given date.
If you need to convert the timestamp to seconds instead, divide the number by
1000
.
const dateStr1 = '2022-06-24'; const date1 = new Date(dateStr1); const timestamp = date1.getTime(); console.log(timestamp); // 👉️ 1656028800000 // 👇️ If you need to convert milliseconds to seconds // divide by 1000 const inSeconds = Math.floor(date1.getTime() / 1000);
If your date string cannot be passed directly to the Date()
constructor, you
can split it on the separator and pass the date components as parameters to the
Date()
constructor.
// ✅ with Date in other format, e.g. MM/DD/YYYY const dateStr = '06/24/2022'; const [month, day, year] = dateStr.split('/'); const date2 = new Date(+year, month - 1, +day); console.log(date2); // 👉️ Fri Jun 24 2022 // 👇️ Get timestamp const timestamp = date2.getTime();
The Date()
constructor is most commonly called with a string formatted as ISO
8601 - YYYY-MM-DDTHH:mm:ss.sssZ
or with parameters for the year
, month
(0
= January, 1 = February, etc.), day of the month
, hours
, minutes
and
seconds
.
In the example above, we had a date string formatted as mm/dd/yyyy
, but this
can be any other format.
We used array destructuring to directly assign the array elements to variables.
And then, we passed the values to the Date()
constructor.
Note that the month parameter is a zero-based value from 0
(January) to 11
(December), so we had to subtract 1
from the value we got from the date
string.
Date
object from the date string, getting a timestamp is as easy as calling the getTime()
method.If you also have time-related data, e.g. hours, minutes and seconds, pass them
to the Date()
constructor as well.
const dateStr = '06/24/2022 09:30:05'; const [dateRelated, timeRelated] = dateStr.split(' '); console.log(dateRelated); // 👉️ "06/24/2022" console.log(timeRelated); // 👉️ "09:30:05" const [month, day, year] = dateRelated.split('/'); const [hours, minutes, seconds] = timeRelated.split(':'); const date2 = new Date(+year, month - 1, +day, +hours, +minutes, +seconds); console.log(date2); // 👉️ Fri Jun 24 2022 09:30:05 // 👇️ Get timestamp const timestamp = date2.getTime();
Then, we repeated the steps from the previous example to get all of the values
we need to pass as parameters to the Date()
constructor.