Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
9
rated 0 times [  16] [ 7]  / answers: 1 / hits: 22480  / 13 Years ago, tue, january 3, 2012, 12:00:00

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


More From » date

 Answers
78

Set the time portion of your created date to zeros.



var d = new Date();
d.setHours(0,0,0,0);

[#88257] Monday, January 2, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
donaldcristianl

Total Points: 114
Total Questions: 95
Total Answers: 110

Location: Bonaire
Member since Sat, May 27, 2023
1 Year ago
;