|
following yesterday's theme, in which I built a dateDiff method for AS2, today I extended the class with a static method to provide dateAdd() functionality. Again, its modeled on the behavior of the CFML dateAdd, so it takes as arguments datePart:String, date:Date, numToAdd:Number. The method returns a new date object. To implement it, you can add the following methods to the class from yesterday.
public static function dateAdd(datePart:String,date:Date,num:Number):Date{
// get date part object;
var dpo : Object = getDateAddPartHashMap()[datePart.toLowerCase()];
// create new date as a copy of date passed in
var newDate : Date = new Date(date.getFullYear(),date.getMonth(),date.getDate(),date.getHours(),date.getMinutes(),date.getSeconds(),date.getMilliseconds());
// set the appropriate date part of the new date
newDate[dpo.set](date[dpo.get]()+num);
// return the new date
return newDate;
}
private static function getDateAddPartHashMap():Object{
var dpHashMap : Object = new Object();
dpHashMap["s"] = new Object();
dpHashMap["s"].get = "getSeconds";
dpHashMap["s"].set = "setSeconds";
dpHashMap["n"] = new Object();
dpHashMap["n"].get = "getMinutes";
dpHashMap["n"].set = "setMinutes";
dpHashMap["h"] = new Object();
dpHashMap["h"].get = "getHours";
dpHashMap["h"].set = "setHours";
dpHashMap["d"] = new Object();
dpHashMap["d"].get = "getDate";
dpHashMap["d"].set = "setDate";
dpHashMap["m"] = new Object();
dpHashMap["m"].get = "getMonth";
dpHashMap["m"].set = "setMonth";
dpHashMap["y"] = new Object();
dpHashMap["y"].get = "getFullYear";
dpHashMap["y"].set = "setFullYear";
return dpHashMap;
}
The complete class now looks like this:
class DateFunction {
/**
dateDiff(datePart:String, date1:Date, date2:Date):Number<BR>
returns the difference between 2 dates<BR>
valid dateParts:<BR>
s: Seconds<BR>
n: Minutes<BR>
h: Hours<BR>
d: Days<BR>
m: Months<BR>
y: Years<BR>
*/
/** dateAdd(datePart:String,date:Date,num:Number):Date<BR>
returns a new date object with the appropriate date/time settings<BR>
*/
public static function dateDiff(datePart:String, date1:Date, date2:Date):Number{
return getDateDiffPartHashMap()[datePart.toLowerCase()](date1,date2);
}
public static function dateAdd(datePart:String,date:Date,num:Number):Date{
// get date part object;
var dpo : Object = getDateAddPartHashMap()[datePart.toLowerCase()];
// create new date as a copy of date passed in
var newDate : Date = new Date(date.getFullYear(),date.getMonth(),date.getDate(),date.getHours(),date.getMinutes(),date.getSeconds(),date.getMilliseconds());
// set the appropriate date part of the new date
newDate[dpo.set](date[dpo.get]()+num);
// return the new date
return newDate;
}
private static function getDateAddPartHashMap():Object{
var dpHashMap : Object = new Object();
dpHashMap["s"] = new Object();
dpHashMap["s"].get = "getSeconds";
dpHashMap["s"].set = "setSeconds";
dpHashMap["n"] = new Object();
dpHashMap["n"].get = "getMinutes";
dpHashMap["n"].set = "setMinutes";
dpHashMap["h"] = new Object();
dpHashMap["h"].get = "getHours";
dpHashMap["h"].set = "setHours";
dpHashMap["d"] = new Object();
dpHashMap["d"].get = "getDate";
dpHashMap["d"].set = "setDate";
dpHashMap["m"] = new Object();
dpHashMap["m"].get = "getMonth";
dpHashMap["m"].set = "setMonth";
dpHashMap["y"] = new Object();
dpHashMap["y"].get = "getFullYear";
dpHashMap["y"].set = "setFullYear";
return dpHashMap;
}
private static function getDateDiffPartHashMap():Object{
var dpHashMap:Object = new Object();
dpHashMap["s"] = getSeconds;
dpHashMap["n"] = getMinutes;
dpHashMap["h"] = getHours;
dpHashMap["d"] = getDays;
dpHashMap["m"] = getMonths;
dpHashMap["y"] = getYears;
return dpHashMap;
}
private static function compareDates(date1:Date,date2:Date):Number{
return date1.getTime() - date2.getTime();
}
private static function getSeconds(date1:Date,date2:Date):Number{
return Math.floor(compareDates(date1,date2)/1000);
}
private static function getMinutes(date1:Date,date2:Date):Number{
return Math.floor(getSeconds(date1,date2)/60);
}
private static function getHours(date1:Date,date2:Date):Number{
return Math.floor(getMinutes(date1,date2)/60);
}
private static function getDays(date1:Date,date2:Date):Number{
return Math.floor(getHours(date1,date2)/24);
}
private static function getMonths(date1:Date,date2:Date):Number{
var yearDiff = getYears(date1,date2);
var monthDiff = date1.getMonth() - date2.getMonth();
if(monthDiff < 0){
monthDiff += 12;
}
if(date1.getDate()< date2.getDate()){
monthDiff-=1;
}
return 12 *yearDiff + monthDiff;
}
private static function getYears(date1:Date,date2:Date):Number{
return Math.floor(getDays(date1,date2)/365);
}
}
I'm working on an application that has as one of it's components a
timesheet. the difficulty is that I'm using Flash forms in Coldfusion MX7
and I'm not sure of all the syntax for doing "date Math". How would I
implement all this great code in a coldfusion flash form?
We have just installed Flex and I am just learning Flex and ActionScript.
I have a new application that requires the use of a dateAdd function, so I
would love to use your code. It looks to me as though it is Java. How can
I save it in an ActionScript (.as) file and use it in my application? I am
sorry if this seems pretty basic, I am totally new to this and just
learning. Thank you for any info you can provide. Virginia Neal
Virginia - Actually, its not java, it is an Actionscript 2.0 class. If you
copy the DateFunction class, paste it in a text editor, and save it in your
classpath (or the same directory as you mxml file) as DateFunction.as, you
can use it in Flex.
Your code works so far very well. Thank you for sharing. I am using it in
Flex project with no problems.
bamm! Nice piece of code! danke! Oh, and nice presentation at MAX 2005
on Flex performance.
Can I just tell you how much you rock in my book right now?? You just saved
me about seventy five minutes of coding for a recurrence function I am
trying to implement. MAJOR kudos to you. Exactly what was needed.
hello, all it's good but, where is dateDiff() with quarter? or week?..
i think it's necesary this options.. please what are you thing about
that...
Great stuff! I wish something like this was provided out of the box. Saved
me a ton of time.