The First and Only Magazine for Macromedia MX™
   
 
Notes from the Flash, Flex and ColdFusion trenches

Category List

Quick Poll

Where are you located?
North America
South America
Europe
Asia
Africa
Australia

My RSS Feeds








Hit Counter

Total: 1,350,937
since: 20 Jan 2005

Search Box

 

Search The Web

Google

MAX 2008

Fun Stuff

Mailing List

Got A Question?

Got A Question?

Leave a comment by the appropriate Entry, or email me

Tag Cloud

                                                                                                                       

DateAdd for ActionScript

posted Wednesday, 1 June 2005

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);
 }
}

tags:    

links: digg this    del.icio.us    technorati    reddit




1. Michael White left...
Tuesday, 16 August 2005 9:46 pm :: http://www.michaelwhitephoto.com

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?


2. Virginia Neal left...
Tuesday, 4 October 2005 5:38 pm

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


3. Jeff Tapper left...
Tuesday, 4 October 2005 10:19 pm

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.

Take a look at the Flex Docs, specifically "Creating Custom Classes with ActionScript 2.0" from the "Flex ActionScript Language Reference" doc.


4. Haluk Tezcan left...
Wednesday, 5 October 2005 12:18 am

Your code works so far very well. Thank you for sharing. I am using it in Flex project with no problems.


5. Douglas Knudsen left...
Friday, 28 October 2005 9:49 am

bamm! Nice piece of code! danke! Oh, and nice presentation at MAX 2005 on Flex performance.

DK


6. Kenny left...
Tuesday, 1 November 2005 12:55 pm

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.


7. Er... left...
Sunday, 11 December 2005 3:06 pm

Ever heard of leap years?


8. theassembler left...
Wednesday, 31 May 2006 8:12 pm

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...


9. Jeff Tapper left...
Tuesday, 6 June 2006 7:37 am

theassembler -

feel free to create your own, I have rarely needed that functionality, so i didnt bother building it in


10. Ben R. left...
Friday, 30 March 2007 2:42 pm

Great stuff! I wish something like this was provided out of the box. Saved me a ton of time.