Jump to content

Recommended Posts

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

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.