Monday, October 6, 2008

Flex DateUtil Class

Upadte- 10/8/2008 1:26 PM
Added DateUtil.create() and DateUtil.clone() static methods. These can be used to work with a existing date. The Clone will cause updates to the original source where as the clone will not.

DateUtil.clone(StartDate.selectedDate).date;

Upadte- 10/10/2008 9:54 AM
Fixed a problem with the getters corrupting the date.


When working with dates and times in flex I found myself wanting to make simple changes like adding a day or trimming off the time witch turned out to be quite a few lines of code. Here is a example of setting a Date Field to yesterday and trimming off the time.
[Bindable]
private var prevDate:Date;

prevDate.hours = prevDate.minutes = prevDate.seconds = prevDate.milliseconds = 0;
prevDate.date--;

// Update the DateField.
dtDate.selectedDate = prevDate;
dtDate.dispatchEvent(new CalendarLayoutChangeEvent(CalendarLayoutChangeEvent.CHANGE));

<mx:datefield id="dtDate" change="" selecteddate="{prevDate}">

As you can see this is a rather large chunk of code to just change the date so I decided to create a DateUtil class. Here is the same operation using the DateUtil class.
<hines:DateField id="dtDate" change="" selectedDate="{DateUtil.now.subtract(DateUtil.DATE, 1).date}"/>

Here are some more examples of the DateUtil class.
var time:Date = DateUtil.now.time;
var monday:Date = DateUtil.now.assign(DateUtil.DAY, 1).date;
var then:Date = DateUtil.now.add(DateUtil.HOURS, 2).datetime;
var day:Number = DateUtil.now.fetch(DateUtil.Day);

Here is the custom class. Feel free to use it just credit me. If you have any problems let me know.
DateUtil.as
package
{
/**
* DateUtil class for simple date manipulation.
*
* You can do simple time alterations with this calss. Here are some samples.
*
* Subtract a day:
* DateUtil.now.subtract(DateUtil.DATE, 1).date;
*
* Add a month:
* DateUtil.now.add(DateUtil.MONTH, 1).date;
*
* Go to Monday:
* DateUtil.now.assign(DateUtil.DAY, 1).date
*
* @author Jeremy Pyne jeremy.pyne@gmail.com
*/
public class DateUtil
{
/**
* Date storage object.
*/
private var _date:Date;

public static const MILLISECONDS:String = "milliseconds";
public static const SECONDS:String = "seconds";
public static const MINUTES:String = "minutes";
public static const HOURS:String = "hours";
public static const DAY:String = "day";
public static const DATE:String = "date";
public static const MONTH:String = "month";
public static const YEAR:String = "fullYear";

/**
* Create a new DateUtil class.
*
* @param date
*/
public function DateUtil(date:Date)
{
_date = date;
}

/**
* Add some value to a variable.
*
* @param variable
* @param value
* @return
*/
public function add(variable:String, value:Number):DateUtil
{
if(variable == DateUtil.DAY)
variable = DateUtil.DATE;

_date[variable] += value;
return this;
}

/**
* Subtract some value to a variable.
*
* @param variable
* @param value
* @return
*/
public function subtract(variable:String, value:Number):DateUtil
{
if(variable == DateUtil.DAY)
variable = DateUtil.DATE;

_date[variable] -= value;
return this;
}

/**
* Set some value to a variable.
*
* @param variable
* @param value
* @return
*/
public function assign(variable:String, value:Number):DateUtil
{
if(variable == DateUtil.DAY)
{
variable = DateUtil.DATE;
value = _date.date + value - _date.day;
}

_date[variable] = value;
return this;
}

/**
* Get a variable.
*
* @param variable
* @return
*/
public function fetch(variable:String):Number
{
return _date[variable];
}

/**
* Get only the current date.
*
* @return
*/
public function get date():Date
{
var date:Date = new Date(_date.time);
date.hours = date.minutes = date.seconds = date.milliseconds = 0;
return date;
}

/**
* Get only the current time.
*
* @return
*/
public function get time():Date
{
var date:Date = new Date(_date.time);
date.fullYear = 1969;
date.month = date.date = 0;
return date;
}

/**
* Get the full date and time.
*
* @return
*/
public function get datetime():Date
{
var date:Date = new Date(_date.time);
return date;
}

/**
* Create a new DateUtil for the current data and time.
*
* @return
*/
public static function get now():DateUtil
{
return new DateUtil(new Date);
}

/**
* Create a new DateUtil for the passed in date. Updates will not effect the original date.
*
* @return
*/
public static function clone(date:Date):DateUtil
{
return new DateUtil(new Date(date.time));
}

/**
* Create a new DateUtil for the passed in date. Updates will alter the original date.
*
* @return
*/
public static function create(date:Date):DateUtil
{
return new DateUtil(date);
}
}
}

No comments: