I have this date format returned from api which is local date
"date":"2023-07-14"
i want to compare this date with the current date
i was trying to compare like this
if(new Date(this.date)==new Date())
but could not find the how to format both the date because both have different formats.
6
Answers
You could just compare only the year, month, and day.
To compare the date returned from the API with the current date both dates must have the same format.
here is an example of code that does this:
I hope my answer is clear to you
Comparing date strings is enough, but you should add your timezone offset:
You can get the current date as a string in
YYYY-MM-DD
format like this:Then simply compare with the API date.
first you should but object in a variable then you should use(>, <, =, and >=) and don’t use(==, !=, ===, or !==)
check this link for more info : https://sentry.io/answers/how-to-compare-two-dates-with-javascript/
You should first convert the date from the API into a Date object and then compare the parts of the date. Like as follows: