Having to dates like this '2022-04-30' & '2022-05-30'
using javascript how can evaluate which dates is lower ? I tried to convert them to milliseconds but with this format date I dont know how
example
if('2022-04-30' < '2022-05-30') {
// true }
CodePudding user response:
You are trying to compare strings not dates. Try this:
const date1 = new Date('2022-04-30');
const date2 = new Date('2022-05-30');
if (date1 < date2) {
console.log('true');
}
Or shorter:
if (new Date('2022-04-30') < new Date('2022-05-30'))
console.log('true');