...

Date Functions

A1ien51
04-17-2003, 03:28 AM
I am working on a series of functions that I keep getting asked about. I finnaly thought I would code some usefull stuff and get away from the crap I have been coding.

The following three scripts do this
--Date Validation
--Calculate Difference Between Dates
--Date Addition or Subtraction

I am sure that these have been done before, but I just filled the last 20 minutes by coding these. I only did a quick browser test, nothing extensive.


<script>

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Date Validation
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function DateValid(mo, dy, yr){
TheDate = mo+"/"+dy+"/"+yr;
Date1 = new Date(TheDate)
D=Date1.getDate();
M=Date1.getMonth()+1;
Y=Date1.getYear();
if(mo!=M || D!=dy || yr!=Y){
alert("Invalid Date Entered");
return false;
}
else return true;
}

//DateValid( month , day , year )
ralph = DateValid(1,13,2003)
if(ralph)alert("valid date");


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Calculate Difference Between Dates
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

var MinM = 1000 * 60; //X = 0 - Answer Minutes
var HrM = MinM * 60; //X = 1 - Answer Hours
var DyM = HrM * 24; //X = 2 - Answer Days
var WkM = DyM * 7; //X = 3 - Answer Weeks
var YrM = DyM * 365; //X = 5 - Answer Years
var MnM = YrM / 12; //X = 4 - Answer Months

var OutMill = new Array(MinM,HrM,DyM,WkM,MnM,YrM);

function DayDiff(mo1, dy1, yr1, mo2, dy2, yr2, X){
Date1 = Date.UTC(yr1, mo1 - 1, dy1)
Date2 = Date.UTC(yr2,mo2 - 1, dy2)
d = new Date();
DateDiff = Date2 - Date1;
Ans = Math.round(DateDiff / OutMill[X]);
return(Ans); //Return difference.
}


//DayDiff( Month1 , Day1 , Year1 , Month2 , Day2 , Year2 , Number of Output Type )
ralph = DayDiff(1,13,2003,5,13,2003,4)
alert(ralph + " months")

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Date Addition or Subtraction
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function DateAdd(mo, dy, yr, ma, da, ya){
TheDate = parseFloat(mo+ma) +"/"+parseFloat(dy+da)+"/"+parseFloat(yr+ya);
Date1 = new Date(TheDate)
D=Date1.getDate();
M=Date1.getMonth()+1;
Y=Date1.getYear();
if(Y<1900)Y+=1900;
TheUpDate= M + "/" + D + "/" + Y;
return TheUpDate
}

//DateAdd(Month, Day , Year , Months to Add , Days to Add, Years to Add)
ralph = DateAdd(1,13,2003,0,13,0)
alert(ralph)

</script>


A1ien51

x_goose_x
04-17-2003, 04:38 AM
http://developer.irt.org/script/date.htm

Alex Vincent
04-18-2003, 02:59 AM
My book, in Chapter Six (I think it's Six), adds a function to Date.prototype for telling you if you're on Daylight Savings time or not.

beetle
04-18-2003, 03:33 AM
Ya, I'd much rather see this as an object or a Date object extension



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum