as3corelib Tutorial:How to Use DateUtil Class in Flex

date-128x128 DateUtil class contains static utility methods for manipulating and working with Dates. This is a useful class to simplify the date processing for programmers. For example, I want to know the day name of date August 10, 1983. What should we do with DateUtil? As we know, DateFiled provide a static method named stringToDate(). So, I get the day name of August 10, 1983 like this: DateUtil. getFullDayName (DateField. stringToDate(“08/10/1983”,”MM/DD/YYYY”)). What should we do without DateUtil?

Search-256x256 Demo | DownloadDownload Full Project

DateBase.dayNamesLong[DateField.stringToDate("08/10/1983","MM/DD/YYYY").getDay()].

stringToDate()    method    

public static function stringToDate(valueString:String, inputFormat:String):Date

Parses a String object that contains a date, and returns a Date object corresponding to the String. The inputFormat argument contains the pattern in which the valueString String is formatted. It can contain "M","D","Y", and delimiter and punctuation characters. The function does not check for the validity of the Date object. If the value of the date, month, or year is NaN, this method returns null.
Parameters:

  valueString:String –  Date value to format.

  inputFormat:String –  String defining the date format.

Returns:
    Date –  The formatted date as a Date object.
Example:
var dob:Date = DateField.stringToDate("06/30/2005", "MM/DD/YYYY");

Screenshot:

DateUtil-01

Click DateField as below to select a date to be compared.

DateUtil-02

Then click the button “compareDates” to compare date1 and date2.

DateUtil-03

Click DateField as below to change the value of date1 to show the results of other methods of DateuUtil class.

DateUtil-04

DateUtil-05

Method

1. compareDates()

public static function compareDates(d1:Date, d2:Date):int

Compares two dates and returns an integer depending on their relationship. Returns -1 if d1 is greater than d2. Returns 1 if d2 is greater than d1. Returns 0 if both dates are equal.

Parameters:

d1:Date The date that will be compared to the second date.

d2:Date The date that will be compared to the first date.

Returns:

int An int indicating how the two dates compare.

2. getAMPM()

public static function getAMPM(d:Date):String

Returns a string indicating whether the date represents a time in the ante meridiem (AM) or post meridiem (PM). If the hour is less than 12 then "AM" will be returned. If the hour is greater than 12 then "PM" will be returned.

Parameters:

d:Date The Date from which to generate the 12 hour clock indicator.

Returns:

String A String ("AM" or "PM") indicating which half of the day the hour represents.

3. getFullDayIndex()

public static function getFullDayIndex(d:String):int

Returns the index of the day that the full day name string represents.

Parameters:

d:String A full day name.

Returns:

int A int that represents that full day represented by the specifed full month name.

4. getFullDayName()

public static function getFullDayName(d:Date):String

Returns the English full day name for the day that the Date represents.

Parameters:

d:Date The Date instance whose day will be used to retrieve the full day name.

Returns:

String An English full day name.

5. getFullMonthIndex()

public static function getFullMonthIndex(m:String):int

Returns the index of the month that the full month name string represents.

Parameters:

m:String A full month name.

6. getFullMonthName()

public static function getFullMonthName(d:Date):String

Returns the English full Month name for the Month that the Date represents.

Parameters:

d:Date The Date instance whose month will be used to retrieve the full month name.

Returns:

String An English full month name.

7. getShortDayIndex()

public static function getShortDayIndex(d:String):int

Returns the index of the day that the short day name string represents.

Parameters:

d:String A short day name.

Returns:

int A int that represents that short day represented by the specifed full month name.

8. getShortDayName()

public static function getShortDayName(d:Date):String

Returns the English Short Day name (3 letters) for the day that the Date represents.

Parameters:

d:Date The Date instance whose day will be used to retrieve the short day name.

Returns:

String An English 3 Letter day abbreviation.

9. getShortHour()

public static function getShortHour(d:Date):int

Returns a short hour (0 – 12) represented by the specified date. If the hour is less than 12 (0 – 11 AM) then the hour will be returned. If the hour is greater than 12 (12 – 23 PM) then the hour minus 12 will be returned.

Parameters:

d:Date The Date from which to generate the short hour.

Returns:

int An int between 0 and 13 ( 1 – 12 ) representing the short hour.

10. getShortMonthIndex()

public static function getShortMonthIndex(m:String):int

Returns the index of the month that the short month name string represents..

Parameters:

m:String The 3 letter abbreviation representing a short month name.

Returns:

int A int that represents that month represented by the specifed short name.

11. getShortMonthName()

public static function getShortMonthName(d:Date):String

Returns the English Short Month name (3 letters) for the Month that the Date represents.

Parametes:

d:Date The Date instance whose month will be used to retrieve the short month name.

Returns:

String An English 3 Letter Month abbreviation.

12. getShortYear()

public static function getShortYear(d:Date):String

Returns a two digit representation of the year represented by the specified date.

Parameters:

d:Date The Date instance whose year will be used to generate a two digit string representation of the year.

Returns:

String A string that contains a 2 digit representation of the year. Single digits will be padded with 0.

13. parseRFC822()

public static function parseRFC822(str:String):Date

Parses dates that conform to RFC822 into Date objects. This method also supports four-digit years (not supported in RFC822), but two-digit years (referring to the 20th century) are fine, too. This function is useful for parsing RSS .91, .92, and 2.0 dates.

Parameters:

str:String Represents the date to be parsed.

Returns:

Date Parsed date.

14. parseW3CDTF()

public static function parseW3CDTF(str:String):Date

Parses dates that conform to the W3C Date-time Format into Date objects. This function is useful for parsing RSS 1.0 and Atom 1.0 dates.

Parameters:

str:String Represents the date to be parsed.

Returns:

Date Parsed date.

15. toRFC822()

public static function toRFC822(d:Date):String

Returns a date string formatted according to RFC822.

Parameters:

d:Date Date to be formatted

Returns:

String Formatted string.

SeeAlso:

16. toW3CDTF()

public static function toW3CDTF(d:Date, includeMilliseconds:Boolean = false):String

Returns a date string formatted according to W3CDTF.

Parameters:

d:Date Date to be formatted.

includeMilliseconds :Boolean (default = false)

Determines whether to include the milliseconds value (if any) in the formatted string.

Returns:

String Formatted string.

The following is full source code:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
	<mx:Script>
		<![CDATA[
			import mx.formatters.DateBase;
			import mx.formatters.DateFormatter;
			import mx.controls.Alert;
			import com.adobe.utils.DateUtil;

			private function doCompareDates():void
			{
				if(date1.selectedDate == null || date2.selectedDate  == null)
				{
					var msg:String = "please select date1 and date2 to compare";
					Alert.show(msg);
					return;
				}
				var result:int = DateUtil.compareDates(date1.selectedDate,date2.selectedDate);

				msg = "compare result is "+result+"\n";
				msg += "it means that date1 is ";

				switch(result)
				{
					case -1:
						msg += "greater than";
						break;
					case 1:
						msg += "smaller than";
						break;
					case 0:
						msg += "equal to";
						break;
				}

				msg += " date2";
				Alert.show(msg);
			}

		]]>
	</mx:Script>

	<mx:VBox width="100%" height="100%">
		<mx:HBox>
			<mx:Label text="date1"/>
			<mx:DateField id="date1" selectedDate="{ new Date()}"/>
			<mx:Text text="{date1.selectedDate.toUTCString()}" />
		</mx:HBox>

		<mx:HBox>
			<mx:Label text="date2"/>
			<mx:DateField id="date2"/>
			<mx:Text text="{date2.selectedDate.toUTCString()}" />
		</mx:HBox>

		<mx:Button label="compareDates" click="doCompareDates();"/>
		<mx:Form visible="{ date1.selectedDate != null}">
			<mx:FormItem label="getAMPM()">
				<mx:Text text="{ DateUtil.getAMPM(date1.selectedDate)}"/>
			</mx:FormItem>
			<mx:FormItem label="getFullDayName()">
				<mx:Text text="{ DateUtil.getFullDayName(date1.selectedDate)}"/>
			</mx:FormItem>
			<mx:FormItem label="getFullDayIndex()">
				<mx:Text text="{ DateUtil.getFullDayIndex(DateUtil.getFullDayName(date1.selectedDate))}"/>
			</mx:FormItem>
			<mx:FormItem label="getFullMonthName()">
				<mx:Text text="{ DateUtil.getFullMonthName(date1.selectedDate)}"/>
			</mx:FormItem>
			<mx:FormItem label="getFullMonthIndex()">
				<mx:Text text="{ DateUtil.getFullMonthIndex(DateUtil.getFullMonthName(date1.selectedDate))}"/>
			</mx:FormItem>
			<mx:FormItem label="getShortDayName()">
				<mx:Text text="{ DateUtil.getShortDayName(date1.selectedDate)}"/>
			</mx:FormItem>
			<mx:FormItem label="getShortDayIndex()">
				<mx:Text text="{ DateUtil.getShortDayIndex(DateUtil.getShortDayName(date1.selectedDate))}"/>
			</mx:FormItem>
			<mx:FormItem label="getShortHour()">
				<mx:Text text="{ DateUtil.getShortHour(date1.selectedDate)}"/>
			</mx:FormItem>
			<mx:FormItem label="getShortMonthName()">
				<mx:Text text="{ DateUtil.getShortMonthName(date1.selectedDate)}"/>
			</mx:FormItem>
			<mx:FormItem label="getShortMonthIndex()">
				<mx:Text text="{ DateUtil.getShortMonthIndex(DateUtil.getShortMonthName(date1.selectedDate))}"/>
			</mx:FormItem>
			<mx:FormItem label="getShortYear()">
				<mx:Text text="{ DateUtil.getShortYear(date1.selectedDate)}"/>
			</mx:FormItem>
			<mx:FormItem label="toRFC822()">
				<mx:Text text="{ DateUtil.toRFC822(date1.selectedDate)}"/>
			</mx:FormItem>
			<mx:FormItem label="parseRFC822()">
				<mx:Text text="{ DateUtil.parseRFC822(DateUtil.toRFC822(date1.selectedDate))}"/>
			</mx:FormItem>
			<mx:FormItem label="toW3CDTF()">
				<mx:Text text="{ DateUtil.toW3CDTF(date1.selectedDate)}"/>
			</mx:FormItem>
			<mx:FormItem label="parseW3CDTF()">
				<mx:Text text="{ DateUtil.parseW3CDTF(DateUtil.toW3CDTF(date1.selectedDate))}"/>
			</mx:FormItem>
		</mx:Form>
	</mx:VBox>
</mx:Application>
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to Ntt.cc

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

2 Responses to “as3corelib Tutorial:How to Use DateUtil Class in Flex”

  1. 只为精品 says:

    as3corelib Tutorial:How to Use DateUtil Class in Flex…

    MonthIndex()
    publicstaticfunctiongetShortMonthIndex(m:String):int
    Returnstheindexofthemonththattheshortmonthnamestringrepresents..
    Parameters:
    m:StringThe3letterabbreviationrepresentingashortmonthname

    DateUtil class contains static utility methods fo…

  2. [...] programming. Here is the summary of the Package com.adobe.utils, include ArrayUtil, DirectoryUtil, DateUtil, IntUtil, NumberFormatter, StringUtil and XMLUtil [...]

Leave a Reply