mwl707 Posted November 1, 2009 Share Posted November 1, 2009 I am trying to compare two dates , I have this code which I thought would work a treat but it didn't . For now I just want to alert an error if the enddate is less than the start date . The date style yyyy-mm-dd needs to be kept in this format for other events prior to this. can anyone give me a clue ? thanks startdate = "2009-11-01" ; enddate = "2009-11-04" ; var d1 = new Date(startdate) var d2 = new Date(enddate) if (d2 < d1) { alert ("Error ! ) ; } document.cookie='st =' + startdate // set sytem cookie document.cookie='en =' + enddate window.location = self.location.href window.opener.location.reload() close() Link to comment https://forums.phpfreaks.com/topic/179831-compare-two-dates/ Share on other sites More sharing options...
Adam Posted November 2, 2009 Share Posted November 2, 2009 You can't pass a date string to the Date() object like that. You can just remove all the date object stuff though and compare between the startdate and enddate variables: var startdate = "2009-11-01"; var enddate = "2009-11-04" ; if (startdate > enddate) { alert ("Error !") ; } (...) If you have a good read up on the Date() object you'll understand how/when to use it better. There was also a missing quote after: "Error ! Link to comment https://forums.phpfreaks.com/topic/179831-compare-two-dates/#findComment-949382 Share on other sites More sharing options...
Psycho Posted November 4, 2009 Share Posted November 4, 2009 You can't pass a date string to the Date() object like that. Not true. You can't pass a date to the Date() object that it does not understand. But, it can parse a string into a date object. But, it only parses dates in the format the humans use, not what machines use yyyy-mm-dd. Here are some acceptable methods of creating a date object from a string (note that some of these may depend on the user's locale setting since many countries use dd-mm-yyyy, whereas the US uses mm-dd-yyyy) var dateObj = new Date('Oct 31, 1968 12:11:32'); var dateObj = new Date('October 31, 1968 12:11:32'); var dateObj = new Date('10/31/1968'); var dateObj = new Date('10/31/68'); Link to comment https://forums.phpfreaks.com/topic/179831-compare-two-dates/#findComment-951198 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.