Flex Date Utils – dateAdd
The first method in this series that I’ll go over is dateAdd.
All of the constants that are referred to in this method are defined in the constants section of the DateUtils class
// Date parts public static const YEAR:String = "fullYear"; public static const MONTH:String = "month"; public static const WEEK:String = "week"; public static const DAY_OF_MONTH:String = "date"; public static const HOURS:String = "hours"; public static const MINUTES:String = "minutes"; public static const SECONDS:String = "seconds"; public static const MILLISECONDS:String = "milliseconds"; public static const DAY_OF_WEEK:String = "day"; /** * Adds the specified number of "date parts" to a date, e.g. 6 days * * @param datePart The part of the date that will be added * @param number The total number of "dateParts" to add to the date * @param date The date on which to add * * @return The new date */ public static function dateAdd( datePart:String, number:Number, date:Date ):Date { var _returnDate:Date = new Date( date ); switch ( datePart ) { case DateUtils.YEAR: case DateUtils.MONTH: case DateUtils.DAY_OF_MONTH: case DateUtils.HOURS: case DateUtils.MINUTES: case DateUtils.SECONDS: case DateUtils.MILLISECONDS: _returnDate[ datePart ] += number; break; case DateUtils.WEEK: _returnDate[ DateUtils.DAY_OF_MONTH ] += number * 7; break; default: /* Unknown date part, do nothing. */ break; } return _returnDate; }
This is a pretty basic method that accepts the part of the date that you want to add to, how much of that date part you want to add, and the date to which you want to add that date part.
To call this method from your class you would have to first import the class
import com.flexoop.utilities.dateutils.DateUtils;
Then from there
private var _changedDate:Date = DateUtils.dateAdd( DateUtils.WEEK, 3, new Date() );
This would add 3 weeks to the current date/time and return that date to _changedDate.
To return a date before the date you pass in, just set the “number” parameter to a negative, and it will subtract that number, like so:
private var _previousDate:Date = DateUtils.dateAdd( DateUtils.YEAR, -2, new Date() );
This would subtract 2 years from the current date and return it to _previousDate.
All methods in this series are describing code in the FlexDateUtils package I put on Google code.
thanks, i´m using it!
Somewhere in this library is a specific tie to a core framework version. Perhaps a embed tag. Causes this error when not using the same core version.
1044: Interface method allowDomain in namespace mx.core:IFlexModuleFactory not implemented by class _ServiceRequestWidget_mx_core_FlexModuleFactory
I’m not actually using any Embed tags in my project. I would have written the entire Flex Date Utils in actionscript if it weren’t for the need of the DateFormatter method to format my dates, and removed the tie to the Flex library altogether
This may be from the included FlexUnit4 or Hamcrest swcs that I’m including with it for unit testing. I’m not actually using Flex4 yet so I’m not able to figure out what I need to do to fix this. If you could narrow down what I needed to modify, I can make the change.