Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare day month and year

Good afternoon in my timezone.

I want to compare two dates , one of them is inserted by the user and the other is the present day. Snippet of code :

    var dateString = "2012-01-03"
    var date = new Date(dateString);
    date < new Date() ? true : false;

This returns true, i think under the hood both Date objects are transformed to milliseconds and then compared , and if it is this way the "Today" object is bigger because of the hours and minutes.So what i want to do is compare dates just by the day month and year.What is the best approach ? Create a new Date object and then reset the hours minutes and milliseconds to zero before the comparison? Or extract the day the month and year from both dates object and make the comparison ? Is there any better approach ?

Thanks in advance With the best regards. Happy new year

like image 502
tt0686 Avatar asked Jan 03 '12 14:01

tt0686


1 Answers

Set the time portion of your created date to zeros.

var d = new Date();
d.setHours(0,0,0,0);
like image 107
epascarello Avatar answered Sep 28 '22 10:09

epascarello