I have a date formatted as 3/13/2022 05:44 PM
.
How can I compare this date with the other dates which are formatted in this manner and check whether they are in the comparative past or future?
Asked
Active
Viewed 1,588 times
0

Bret Weinraub
- 1,943
- 15
- 21

raisan jmr
- 47
- 1
- 9
-
1Parse the string to a comparable format. This could be the Unix timestamp or the ISO 8601 timestamp – jabaa Mar 13 '22 at 12:25
-
Try https://date-fns.org/ or https://moment.github.io/luxon/ for parsing, since the built-in `Date.parse` can be difficult to use. – Moshe Katz Mar 13 '22 at 12:28
1 Answers
1
Ohkay, so you need to compare the two dates and find which is latest, that is past or future..
So, you can just convert the dates to milliseconds using the Date.parse() function. This will parses a date string and returns the number of milliseconds since midnight January 1, 1970.
And then compare the two numbers accordingly.. For example:
<p id="demo"></p>
<script>
let ms = Date.parse("3/13/2022 05:47 PM");
let ns = Date.parse("3/13/2022 04:47 AM");
if(ms > ns){
document.getElementById("demo").innerHTML = "3/13/2022 05:47 PM is Future";
}else{
document.getElementById("demo").innerHTML = "3/13/2022 04:47 AM is Future";
}

Hemant Kushwah
- 51
- 1
- 2