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?

3

3 Answers 3

2

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"

Sign up to request clarification or add additional context in comments.

1 Comment

The right format should be uppercase: "DD-MM-YYYY"
1

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

Comments

1

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)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.