Archive

Archive for the ‘FlexDateUtils’ Category

Flex Date Utils – daysInMonth

December 9th, 2008 Gareth 3 comments
/**
 * Gets the days in the month
 * 
 * @param date	The date to check
 * 
 * @return		The number of days in the month
 */
public static function daysInMonth( date:Date ):Number {
	// get the first day of the next month
	var _localDate:Date = new Date( date.fullYear, DateUtils.dateAdd( DateUtils.MONTH, 1, date ).month, 1 );
	// subtract 1 day to get the last day of the requested month
	return DateUtils.dateAdd( DateUtils.DAY_OF_MONTH, -1, _localDate ).date;
}

I have found method to be quite useful. It returns the number of days in a month based upon the date passed in.

First I create a date to represent the first of the following month, then I subtract 1 day from that month, and return the day that that date represents, which is now the last day of the month requested.

To use this method, first import the package:

import com.flexoop.utilities.dateutils.DateUtils;

and then:

private var _dim:Number = DateUtils.daysInMonth( new Date( 2008, 1 ) );

_dim will now contain 29, as there were 29 days in February, 2008.

All methods in this series are describing code in the FlexDateUtils package I put on Google code.

Flex Date Utils – dayOfWeek

December 8th, 2008 Gareth No comments

These methods are somewhat short so I’ll be going over dayOfWeek, and toFlexDayOfWeek

/**
 * Gets the day of the week
 * 
 * @param date	The date for which to get the day of the week
 * 
 * @return		A number representing the day of the week
 */
public static function dayOfWeek( date:Date ):Number {
	return date.getDay();
}

This one is perhaps not as useful as some of the others, as it can be called directly from the date object itself, but it is contained in the ColdFusion date functions, so I thought I would add it to the FlexDateUtils package, and for me, it is easier to remember DateUtils.dayOfWeek than date.getDay().

It accepts a date and then returns the day of the week as a number that that date represents. Valid return values would be 0 to 6.

To call this method, import the package:

import com.flexoop.utilities.dateutils.DateUtils;

and then:

private var _dow:Number = DateUtils.dayOfWeek( new Date( 2008, 4, 1 ) );

This would return 4 as May 1, 2008 ( dates in Flex also start from zero ) was a Thursday.

/**
 * Converts the day of the week to a Flex day of the week
 * 
 * @param date	The human readable day of week
 * 
 * @return		The Flex converted day of week or 0 aka Sunday
 */
public static function toFlexDayOfWeek( localDayOfWeek:Number ):Number {
	return ( localDayOfWeek > 0 && localDayOfWeek < 8 ) ? localDayOfWeek - 1 : 0;
}

This method is really just a simplification of converting the day of the week from what most people think of, 1 to 7, to what Flex considers a day of the week, 0 to 6. I have another method that I will document in a later post that will convert the string version of a day of the week, to its numeric equivalent.

To call this method, import the package:

import com.flexoop.utilities.dateutils.DateUtils;

and then:

private var _flexDow:Number = DateUtils.toFlexDayOfWeek( 4 );

This would return 3 as 4 (Wednesday, starting from Sunday), would be 3 to Flex.

All methods in this series are describing code in the FlexDateUtils package I put on Google code.

Flex Date Utils – dateAdd

December 7th, 2008 Gareth 6 comments

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.

Flex Date Utils – Overview

December 6th, 2008 Gareth No comments

This will be the start of a new series of blog posts for me focused around my flexdateutils library that I’ve published to Google code at http://code.google.com/p/flexdateutils/

I first began writing this code in response to some of what I felt were shortcomings to the Flex framework in handling date manipulation. It seemed that just about every other programming language had its own library of date functions, but the few that Flex had made it very difficult to do anything to a date unless you managed it yourself. And so was born FlexDateUtils.

Each day I will try to post at least one method from the library and explain the code a little. If it’s a very basic method, I’ll probably add a couple more methods. Most of the methods were created from ColdFusion’s date library as those have been very useful to me whenever I have had any kind of date manipulation. I have very rarely had to add any of my own code to get back what I needed from ColdFusion’s date functions.

I am hoping this will be a useful group of functions to other developers, and, who knows, perhaps Adobe will even think that it is something that should be added to their standard Flex framework :)