3

I am trying to compare two dates in a VueJs application. I need to show whether the selected date is higher / not than today's date.

I applied separate functionalities to get today's date and the selected date. Both return the date in dd-mm-yyyy format.

When I compare those two dates using,

if (dateEntered < currentDate) {

    }

it is not working. How do I compare the two dates?

NSR
  • 315
  • 2
  • 5
  • 18

3 Answers3

3

In this case, moment come in handy. Like below

var date1 = moment(dateEntered).format("dd-mm-yyyy")
var date2 = moment(currentDate).format("dd-mm-yyyy")
if(date1 >  date2){
    //Do your thing
} else {

}

You need to install moment and import it import moment from "moment"

muya.dev
  • 966
  • 1
  • 13
  • 34
2

Just to elaborate on previous answer, if you're using a date library (moment or date-fns for example), you can use their utility function :

moment('2010-10-20').isBefore('2010-10-21'); // true

Pierre_T
  • 1,094
  • 12
  • 29
0

If anyone using the dayjs than you can use this function. It will return true or false. https://day.js.org/docs/en/plugin/is-same-or-before

export function isSameBefore(fromDate, toDate) {
  return dayjs(fromDate).isSameOrBefore(toDate)
}