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

Anderson, John A. art at uic.edu
Thu Dec 4 11:31:26 MST 2014


Bob,

Try this simple modification to Ted's original function, (add after "var
date1 = new Date();" to zero out the time part of the date object),

date1.setHours(0,0,0,0);

original function:
http://jsfiddle.net/qpvugLna/1/ (warning, it does show the alert when you
visit the page)

modified function:
http://jsfiddle.net/5afeLdww/ (ditto warning)

Art

On Thu, December 4, 2014 9:58 am, Bob Patin wrote:
> Hi,
>
> I did see it, and I tried that as well; I tried changing the dates to
> milliseconds, thinking that would allow me to do comparisons, since I read
> somewhere that you can’t use == to compare 2 dates.
>
> Just now, thinking I’d waste yet more time on it, I tried this:
>
> var date1 = new Date();
> var date2 = new Date("12/04/2014");
> var res = compareDates(date1,date2);
> alert(res);
>
> and the compareDates function is this:
>
> function compareDates(date1, date2){
>
> 	date1 = date1.getMilliseconds();
> 	date2 = date2.getMilliseconds();
> 	alert (date1 + “---" + date2);
>
> 	if (date1 < date2){
> 		return 0;
> 	}else if (date1 == date2){
> 		return 1;
> 	}else if (date1 > date2){
> 		return 2;
> 	}else{
> 		return "UNSOLVED";
> 	}
> }
>
> When I run it, the alert gives me a value for date1 but not for date2.
>
> Why am I not getting a value from date2? Can I not use the format that
> I’m using in the call to the function?
>
> 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
>
>> On Dec 4, 2014, at 9:18 AM, Fitzgerald, Theodore C
>> <ted-fitzgerald at uiowa.edu> wrote:
>>
>> Not sure if you missed my post, so here it is again:
>>
>> In order to do the greater than and less than comparison you'll need to
>> be working with Date objects, so if getToday() doesn't return a date
>> object, your function won't work correctly.
>>
>> I'm not sure what "getToday();" is doing, but to get today's date,  you
>> only need to do "new Date()";.  Here is an example of it working:
>>
>> http://jsfiddle.net/qpvugLna/1/ <http://jsfiddle.net/qpvugLna/1/>
>> (warning, it does show the alert when you visit the page)
>>
>
> _______________________________________________
> FX.php_List mailing list
> FX.php_List at mail.iviking.org
> http://www.iviking.org/mailman/listinfo/fx.php_list
>


-- 
John A. "Art" Anderson
University of Illinois at Chicago




More information about the FX.php_List mailing list