[FX.php List] [OFF] Javascript question--comparing dates
Ness, David
dness at bondedbuilders.com
Wed Dec 3 13:05:39 MST 2014
Bob,
This is what I use to compare dates. Hope this helps:
------------------------------------------
function compareDates(value1, value2) {
/* A simple function that takes two well formed dates.
The function will compare the dates and return:
0 if the dates are same
-1 if the first one is an earlier date
1 if the first one is a later date
*/
var date1, date2;
var month1, month2;
var year1, year2;
month1 = value1.substring (0, value1.indexOf (dtCh));
date1 = value1.substring (value1.indexOf (dtCh)+1, value1.lastIndexOf (dtCh));
year1 = value1.substring (value1.lastIndexOf (dtCh)+1, value1.length);
month2 = value2.substring (0, value2.indexOf (dtCh));
date2 = value2.substring (value2.indexOf (dtCh)+1, value2.lastIndexOf (dtCh));
year2 = value2.substring (value2.lastIndexOf (dtCh)+1, value2.length);
if(month1.length == 1) {month1 = '0' + month1}
if(date1.length == 1) {date1 = '0' + date1}
if(month2.length == 1) {month2 = '0' + month2}
if(date2.length == 1) {date2 = '0' + date2}
if (year1 > year2) return 1;
else if (year1 < year2) return -1;
else if (month1 > month2) return 1;
else if (month1 < month2) return -1;
else if (date1 > date2) return 1;
else if (date1 < date2) return -1;
else return 0;
}
// example on calling the function isDate()
function ValidateDate(){
var dt=document.frmSample.txtDate
if (isDate(dt.value)==false){
dt.focus()
return false
}
return true
}
--------------------------------------------
David Ness
FileMaker & Web Applications Developer
Bonded Builders Warranty Group
St. Petersburg, Florida USA
800-749-0381 x4923
-----Original Message-----
From: fx.php_list-bounces at mail.iviking.org [mailto:fx.php_list-bounces at mail.iviking.org] On Behalf Of Bob Patin
Sent: Wednesday, December 03, 2014 2:18 PM
To: FX.php Discussion List
Subject: [FX.php List] [OFF] Javascript question--comparing dates
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