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

Fitzgerald, Theodore C ted-fitzgerald at uiowa.edu
Thu Dec 4 09:07:37 MST 2014


Bob,

Sorry, I didn’t take into account that doing “date1 = new Date();” also includes the time, while with “date2 = new Date(“12/04/2014”);” the time is at midnight (I believe), so technically date1 is greater than date2 in this instance.

Try this:

var date1 = "12/04/2014";
var date2 = new Date("12/04/2014");
var res = compareDates(date1,date2);
alert(res);

function isDateObject(val) {
    if (val === null) { return false;}
    return ( Object.prototype.toString.call(val) === '[object Date]');
}

function compareDates(date1, date2){
    if (!isDateObject(date1)) { date1 = new Date(date1);}
    if (!isDateObject(date2)) { date2 = new Date(date2);}
                if (+date1 < +date2){
                                return 0;
                }else if (+date1 == +date2){
                                return 1;
                }else if (+date1 > +date2){
                                return 2;
                }else{
                                return "UNSOLVED";
                }
}

Ted

Application Developer
ITS-Enterprise Services
2800 UCC
University of Iowa

From: fx.php_list-bounces at mail.iviking.org [mailto:fx.php_list-bounces at mail.iviking.org] On Behalf Of Bob Patin
Sent: Thursday, December 04, 2014 9:59 AM
To: FX.php Discussion List
Subject: Re: [FX.php List] [OFF] Javascript question--comparing dates

I also tried this:

date2 = new Date(2014,12,4);

But that doesn’t work either.

On Dec 4, 2014, at 9:58 AM, Bob Patin <bob at patin.com<mailto:bob at patin.com>> 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<mailto:bob at longtermsolutions.com>
615-333-6858
FileMaker 9, 10, 11, 12 & 13 Certified Developer
http://www.longtermsolutions.com<http://www.longtermsolutions.com/>
-
iChat: bobpatin at me.com<mailto: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<mailto: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/ (warning, it does show the alert when you visit the page)



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.iviking.org/pipermail/fx.php_list/attachments/20141204/84470739/attachment-0001.html


More information about the FX.php_List mailing list