[FX.php List] [OFF] Javascript question--comparing dates

Chris Hansen chris at iViking.org
Thu Dec 4 10:33:00 MST 2014


Hey Bob,

A couple thoughts:

1) I know I’ve had trouble in the past some places with a leading zero on the day when using m/d/Y formatting;

2) Not all browsers accept the same date formats as strings when using the Date() constructor;

3) Based on the above, I’d personally pass the year, month, and day separately as integers, rather than a single string.

HTH

—-Chris

> On Dec 3, 2014, at 12:18 PM, Bob Patin <bob at patin.com> wrote:
> 
> OK, list, this should be really easy, but I can’t get the proper results:
> 
> I have this code:
> 
> var date1 = getToday();
> var date2 = new Date("12/01/2014");
> var res = compareDates(date1,date2);
> alert(res);
> 
> Here’s the function:
> 
> // compare any 2 dates
> 	// 0 -- date1 < date2
> 	// 1 -- dates are equal
> 	// 2 -- date1 > date2
> 
> function compareDates(date1, date2){
> 	if (+date1 < +date2){
> 		return 0;
> 	}else if (+date1 == +date2){
> 		return 1;
> 	}else if (+date1 > +date2){
> 		return 2;
> 	}else{
> 		return "UNSOLVED";
> 	}
> } 
> 
> If you try this, you’ll get UNSOLVED rather than the right answer, which should be 2, because date1 > date2 in my example.
> 
> This is so easy to do in PHP, but for some reason, despite lots of Googling, I haven’t gotten anything that works.
> 
> Thanks for any enlightenment,
> 
> Bob Patin
> Longterm Solutions
> bob at longtermsolutions.com
> 615-333-6858
> FileMaker 9, 10, 11, 12 & 13 Certified Developer
> http://www.longtermsolutions.com
> -
> iChat: bobpatin at me.com
> Twitter: bobpatin
>> FileMaker Consulting 
> FileMaker Hosting for all versions of FileMaker
> PHP • Full email services • Free DNS hosting • Colocation • Consulting
> 
> 
> _______________________________________________
> FX.php_List mailing list
> FX.php_List at mail.iviking.org
> http://www.iviking.org/mailman/listinfo/fx.php_list
> 



More information about the FX.php_List mailing list