Date Arithmetic Functions in eScript
The AddToDate function allows you to take a date value and add or subtract days, hours, minutes, and seconds.
function AddToDate(sourceDate, nDays, nHours, nMinutes, nSeconds, nsign)
{
// Parameters :
// sourceDate : Date object
// nDays, nHours , nMinutes , nSeconds : Integer numbers
// nsign : Can have two values : 1 or -1
// 1 indicates to ADD to the sourceDate
// -1 indicates to SUBTRACT from the sourceDate
// Returns : A date object, after adding/subtracting
// ndays,hNours,nMinutes
// and nseconds to the sourceDate.
var retDate = sourceDate;
retDate.setDate(retDate.getDate()+nsign*nDays);
retDate.setHours(retDate.getHours()+nsign*nHours);
retDate.setMinutes(retDate.getMinutes()+nsign*nMinutes);
retDate.setSeconds(retDate.getSeconds()+nsign*nSeconds);
return (retDate)
}
The DiffDays function calculates the number of days between two date values.
function DiffDays(date1, date2)
{
// Parameters :
// date1 : Date object
// date2 : Date object
// Returns : Number of days between date1 and date2
return ((date2.getTime()-date1.getTime())/(1000*60*60*24));
}
The DateToString function reformats a date value as a string with the format "mm/dd/yyyy" or "mm/dd/yyyy hh:mm:ss".
function DateToString(dDate)
{
// Parameters :
// dDate : Date object
// Returns : A string with the format "mm/dd/yyyy" or "mm/dd/yyyy hh:mm:ss"
var sMonth = ToString(dDate.getMonth() + 1);
if (sMonth.length == 1) {sMonth = "0" + sMonth;}
var sDay = ToString(dDate.getDate());
if (sDay.length == 1) {sDay = "0" + sDay;}
var sHours = ToString(dDate.getHours());
if (sHours.length == 1) {sHours = "0" + sHours;}
var sMinutes = ToString(dDate.getMinutes());
if (sMinutes.length == 1) {sMinutes = "0" + sMinutes;}
var sSeconds = ToString(dDate.getSeconds());
if (sSeconds.length == 1) {sSeconds = "0" + sSeconds;}
if (sHours == "00" && sMinutes == "00" && sSeconds == "00")
return (sMonth +"/"+ sDay +"/" + dDate.getFullYear())
else
return (sMonth +"/"+ sDay +"/" + dDate.getFullYear() +" "+sHours+":"+sMinutes+":"+sSeconds);
}
The StringToDate function takes a date value which is formatted as a string and returns a date object.
function StringToDate(sDate)
{
// Parameters :
// sDate : A string with the format "mm/dd/yyyy" or "mm/dd/yyyy hh:mm:ss"
// Returns : a Date object
var ArDateTime = sDate.split (" ");
var ArDate = ArDateTime[0];
var splitDate = ArDate.split ("/");
var nDay = ToNumber(splitDate[1]);
var nMonth = ToNumber(splitDate[0]);
var nYear = ToNumber(splitDate[2]);
if (ArDateTime.length == 1)
return (new Date(nYear, nMonth-1 , nDay))
else
{ var ArTime = ArDateTime[1];
var splitTime = ArTime.split(":");
if (splitTime[0]=="00" && splitTime[1]=="00" && splitTime[2]=="00" )
return (new Date(nYear, nMonth-1 , nDay))
else
{
var nHours = ToNumber(splitTime[0]);
var nMinutes = ToNumber(splitTime[1]);
var nSeconds = ToNumber(splitTime[2]);
return (new Date(nYear,nMonth-1,nDay, nHours, nMinutes, nSeconds))
}
}
}
No comments:
Post a Comment