Need Flex Migration Help?
- Adobe ends Flash player support in 2020
- Flex apps will no longer function
- We can help migrate your app
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);
}
}
slight change to getMonths to make this work as expected:
private static function getMonths(date1:Date,date2:Date):Number
{
var yearDiff :Number = getYears(date1,date2);
var monthDiff :Number = date1.getMonth() – date2.getMonth();
if (date2.getFullYear() – date1.getFullYear() <= 0)
{
if (monthDiff < 0)
monthDiff += 12;
}
else
{
if (monthDiff <= 0)
monthDiff += 12;
}
if (date1.getDate() < date2.getDate())
monthDiff-=1;
return 12 *yearDiff + monthDiff;
}
If you compare the following dates and use: dateDiff(“m”, date2, date1);
returns: 0
Wed Nov 17 05:11:00 GMT-0800 2010
Thu Nov 17 00:00:00 GMT-0800 2011
returns: -1
Wed Nov 17 05:11:00 GMT-0800 2010
Mon Nov 14 00:00:00 GMT-0800 2011
returns: 11
Wed Nov 17 05:11:00 GMT-0800 2010
Wed Nov 14 00:00:00 GMT-0800 2012
returns: 23
Wed Nov 17 05:11:00 GMT-0800 2010
Thu Nov 14 00:00:00 GMT-0800 2013
Consider the following method to replace getMonths:
private static function getMonths(date1:Date,date2:Date):Number
{
var yearDiff :Number = getYears(date1,date2);
var monthDiff :Number = date1.getMonth() – date2.getMonth();
var fullYearDiff :Number = date1.getFullYear() – date2.getFullYear();
var compareDate1 :Date = new Date(date1);
var compareDate2 :Date = new Date(date2);
if (date1.getDate() == date2.getDate() && yearDiff >= 1)
yearDiff -= 1;
if (date1.getDate() > date2.getDate())
{
compareDate1 = new Date(date2);
compareDate2 = new Date(date1);
}
if (compareDate1.getFullYear() – compareDate2.getFullYear() <= 0)
{
if(monthDiff < 0){
monthDiff += 12;
}
}
else
{
if(monthDiff <= 0){
monthDiff += 12;
}
}
if (date1.getDate() < date2.getDate())
monthDiff-=1;
return 12 *yearDiff + monthDiff;
}