Andrus Andrus - 1 month ago 4x
Javascript Question

How to find if date if before current date in javascript

Dates are stored in javascript as yyyy-mm-dd strings whose time part is always 0 like

todays date is


How to find if date is before current date ?
I tried

alert(new Date("2016-09-04T00:00:00") < new Date());

but this returns true with is wrong if today is 2016-09-04

new Date()
returns time also and this produces wrong result.

How to compare date part only ?

Should todays date converted to string and substring used to compare only first 10 characters of strings or is there something better ?

I read answers from referenced question but havent found clear answer to this question. Those answers describe generic solution for date equality comparison.
This questions asks how to find if date part is before current date.


You are missing the timezone offset. Try this:

dateA = new Date("2016-09-05T00:00:00");
offset = dateA.getTimezoneOffset() * 1000 * 60;
dateA = dateA.getTime() + offset;
dateB = new Date().setHours(0,0,0,0);

I've changed it to the 5th (it's already this day in my timezone. Please amend it, if necessary)