Need Flex Migration Help?
- Adobe ends Flash player support in 2020
- Flex apps will no longer function
- We can help migrate your app
One of the tools I often find myself needing when developing applications in ActionScript is a utility for determining the difference between 2 dates. Following the structure of ColdFusion’s DateDiff function, I came up with 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>
*/
public static function dateDiff(datePart:String, date1:Date, date2:Date):Number{
return getDatePartHashMap()[datePart.toLowerCase()](date1,date2);
}
private static function getDatePartHashMap():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);
}
}
there is only one public method to interact with, that is dateDiff. You can use it like this:
var now:Date = new Date();
var kaliBDay = new Date(2004,5,23,11,17);
trace(“Diff between “+ kaliBDay + ” and now ” + DateFunction.dateDiff(“y”,now,kaliBDay) +” in years”);
Following the lead from the CF function, you can pass in dateParts of “y” for years, “m” for months, “d” for days, “h” for hours, “n” for minutes or “s” for seconds. If you want to add new date parts, simply right the method, and reference it in the getDatePartHashMap() method. For example, to add a new date part “ms” for milliseconds, I’d add one more line to getDatePartHashMap which said:
dpHashMap[“ms”] = compareDates;
This can be done, as the compareDates method is already written to determine the number of milliseconds between any two dates.
Most of the methods are pretty straight forward, the getMonths is more complex, as the number of days in any given month is different, so it wasnt a simple division based on the millisecond difference, like all the other calculations were.
I’ve extended this class a bit more, and added dateAdd() functionality to it as well.