-2

Let say I have one field resolved.time. resolved.time="07/06/17 14:19:39"

I would like to write a condition on javascript like this:

if resolved.time>24 hours{
print("true");
}
else
{
print("false");
}

How can i write this condition "if resolved.time>24 hours" on JS? Appreciate for anything help.

Thanks and regards, Okik

  • How can a point in time (a date) be "greater than" a period of time? That's like saying "is tomorrow less than The Eighties?" or worse "is an onion longer than all the cheeses?" – Caius Jard Jul 24 '17 at 10:55
  • yes i means, greater than a period of field resolved time, means tomorrow. – user3418286 Jul 24 '17 at 11:05

1 Answers1

-1

let resolved = {}
resolved.time = "07/06/17 14:19:39"

// convert resolved.time to milliseconds
const time = (new Date(resolved.time)).getTime()

const dayInMs = 24 * 60 * 60 * 1000


// check if duration between now and `time is greater than 24h 
if (Date.now() - time > dayInMs) {
  console.log("true");
} else {
  console.log("false");
}
marzelin
  • 10,790
  • 2
  • 30
  • 49
  • Please do not recommend parsing strings with the built-in parser, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Jul 24 '17 at 22:26
  • @RobG I didn't know about `Date.parse()`. Thanks for the tip. – marzelin Jul 25 '17 at 07:19