Date part to milliseconds.

I've this time 2023-05-25T11:18:45.752Z . How do I get only the date part and change it to milliseconds. I know I can change the whole datetime to seconds by
const date = new Date().getTime();
const date = new Date().getTime();
But this takes the full datetime.
8 Replies
13eck
13eck17mo ago
You pass the date string into the new Date() constructor. Also, the .getTime() method return the number of milliseconds since January 1, 1970, UTC not the number of seconds. So you'd do new Date("2023-05-25T11:18:45.752Z").getTime() Or you could do Date.parse("2023-05-25T11:18:45.752Z"). Either way, you'd get the returned value of 1685013525752 ms since Epoc 0
Malik
Malik17mo ago
Ok understood. But is .getTime() the right thing to use. I want my given time to be changed into milliseconds. As you said it gives the number of milliseconds since Jan 1, 1970.
13eck
13eck17mo ago
That's what timestamps are, number of (m)s since Jan 1 1970. What are you wanting to do?
Malik
Malik17mo ago
I just want them to be changed to milliseconds. I got a function where I'm comparing dates that's why need the whole date into milliseconds. But it's working for me so I got no complains. Just a question what if the date value is less than jan 1 1970.
13eck
13eck17mo ago
Then it's a negative number But if you want to change a time to ms, then use either of the two methods I shared above. In JS all timestamps are in ms so there's no conversion or hidden trick needed. If you want less-precise than ms that's when you start making functions to do maths
Malik
Malik17mo ago
ok got it. Thank you 💛
13eck
13eck17mo ago
👍
Want results from more Discord servers?
Add your server