kittu
kittu

Reputation: 7018

How to compare two utc datetimes in javascript?

I have a date like this in DB:

  1. 2020-10-21T00:06:06.000Z which I want to compare with current date and time

If current date and time has past the date from DB then trigger some action.

let oldDate = `2020-10-21T00:06:06.000Z`;
let dateNow = new Date();
console.log('oldDate ', new Date(oldDate));
console.log('date now', dateNow);
console.log('oldDate time', new Date(oldDate).getTime());
console.log('newDate time', dateNow.getTime());

const schedule = dateNow.getTime() > new Date(oldDate).getTime() ? "trigger now" : "trigger later";
console.log('scheduled for:  ', schedule);

I am trying to check if dateTime is past old dateTime but is this is right approach or any other better solutions?

Upvotes: 0

Views: 838

Answers (1)

B_Joker
B_Joker

Reputation: 119

Sorry, not enough reputation to make a comment. Did you try this?

let oldDate = '2020-10-21T00:06:06.000Z';
const schedule = Date.now() - Date.parse(oldDate) > 0 ? "trigger now" : "trigger later";

Upvotes: 1

Related Questions