return returnString;
}
/**
Converts W3C ISO 8601 date strings into a Date object.
@param iso8601: A valid ISO 8601 formatted String.
@return Returns a Date object of the specified date and
time of the ISO 8601 string in universal time.
@see <a href="http://www.w3.org/TR/NOTE-datetime">W3C IS
O 8601 specification</a>
@example
<code>
trace(DateUtil.iso8601ToDate("1994-11-05
T08:15:30-05:00").toString());
</code>
*/
public static function iso8601ToDate(iso8601:String):Date {
var parts:Array = iso8601.toUpperCase().split('T');
var date:Array = parts[0].split('-');
var time:Array = (parts.length <= 1) ? new Array()
: parts[1].split(':');
var year:uint = ObjectUtil.isEmpty(date[0]) ? 0 :
Number(date[0]);
var month:uint = ObjectUtil.isEmpty(date[1]) ? 0 :
Number(date[1] - 1);
var day:uint = ObjectUtil.isEmpty(date[2]) ? 1 :
Number(date[2]);
var hour:int = ObjectUtil.isEmpty(time[0]) ? 0 :
Number(time[0]);
var minute:uint = ObjectUtil.isEmpty(time[1]) ? 0 :
Number(time[1]);
var second:uint = 0;
var millisecond:uint = 0;
if (time[2] != undefined) {
var index:int = time[2].length;
var temp:Number;
if (time[2].indexOf('+') > -1)
index = time[2].indexOf('+');
else if (time[2].indexOf('-') > -1)
index = time[2].indexOf('-');
else if (time[2].indexOf('Z') > -1)
index = time[2].indexOf('Z');
if (isNaN(index)) {
temp = Number(time[2].slice(0, in
dex));
second = Math.floor(temp);
millisecond = 1000 * ((temp % 1) / 1);
}
if (index != time[2].length) {
var offset:String = time[2].slice(in
dex);
var userOffset:Number = DateUtil.getDiff
erenceFromUTCInHours(new Date(year, month, day));
switch (offset.charAt(0)) {
case '+' :
case '-' :
hour -= userOffset + Num
ber(offset.slice(0));
break;
case 'Z' :
hour -= userOffset;
break;
}
}
}
return new Date(year, month, day, hour, minute, second,
millisecond);
}
/**
Converts the month number into the full month name.
@param month: The month number (0 for January, 1 for Feb
ruary, and so on).
@return Returns a full textual representation of a month
, such as January or March.
@example
<code>
var myDate:Date = new Date(2000, 0, 1);
trace(DateUtil.getMonthAsString(myDate.g
etMonth())); // Traces January
</code>
*/
public static function getMonthAsString(month:Number):String {
var monthNamesFull:Array = new Array('January', 'Februar
y', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', '
November', 'December');
return monthNamesFull[month];
}
/**
Converts the month number into the month abbreviation.
@param month: The month number (0 for January, 1 for Feb
ruary, and so on).
@return Returns a short textual representation of a mont
h, three letters.
@example
<code>
var myDate:Date = new Date(2000, 0, 1);
trace(DateUtil.getMonthAbbrAsString(myDa
te.getMonth())); // Traces Jan
</code>
*/
public static function getMonthAbbrAsString(month:Number):String
{
return DateUtil.getMonthAsString(month).substr(0, 3);
}
/**
Converts the day of the week number into the full day na
me.
@param day: An integer representing the day of the week
(0 for Sunday, 1 for Monday, and so on).
@return Returns a full textual representation of the day
of the week.
@example
<code>
var myDate:Date = new Date(2000, 0, 1);
trace(DateUtil.getDayAsString(myDate.get
Day())); // Traces Saturday
</code>
*/
public static function getDayAsString(day:Number):String {
var dayNamesFull:Array = new Array('Sunday', 'Monday', '
Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
return dayNamesFull[day];
}
/**
Converts the day of the week number into the day abbrevi
ation.
@param day: An integer representing the day of the week
(0 for Sunday, 1 for Monday, and so on).
@return Returns a textual representation of a day, three
letters.
@example
<code>
var myDate:Date = new Date(2000, 0, 1);
trace(DateUtil.getDayAbbrAsString(myDate
.getDay())); // Traces Sat
</code>
*/
public static function getDayAbbrAsString(day:Number):String {
return DateUtil.getDayAsString(day).substr(0, 3);
}
/**
Finds the number of days in the given month.
@param year: The full year.
@param month: The month number (0 for January, 1 for Feb
ruary, and so on).
@return The number of days in the month; 28 through 31.
@example
<code>
var myDate:Date = new Date(2000, 0, 1);
trace(DateUtil.getDaysInMonth(myDate.get
FullYear(), myDate.getMonth())); // Traces 31
</code>
*/
public static function getDaysInMonth(year:Number, month:Number)
:uint {
return (new Date(year, ++month, 0)).getDate();
}
/**
Determines if time is Ante meridiem or Post meridiem.
@param hours: The hour to find the meridiem of (an integ
er from 0 to 23).
@return Returns either <code>"AM"</code> or <code>"PM"</
code>
@example
<code>
trace(DateUtil.getMeridiem(17)); // Trac
es PM
</code>
*/
public static function getMeridiem(hours:Number):String {
return (hours < 12) ? 'AM' : 'PM';
}
/**
Determines the difference between two dates.
@param startDate: The starting date.
@param endDate: The ending date.
@return Returns the difference in milliseconds between t
he two dates.
@example
<code>
trace(ConversionUtil.millisecondsToDays(
DateUtil.getTimeBetween(new Date(2007, 0, 1), new Date(2007, 0, 11)))); // Trace
s 10
</code>
*/
public static function getTimeBetween(startDate:Date, endDate:Da
te):Number {
return endDate.getTime() - startDate.getTime();
}
/**
Determines the time remaining until a certain date.
@param startDate: The starting date.
@param endDate: The ending date.
@return Returns an Object with the properties <code>days
</code>, <code>hours</code>, <code>minutes</code>, <code>seconds</code> and <cod
e>milliseconds</code> defined as numbers.
@example
<code>
var countdown:Object = DateUtil.getCount
downUntil(new Date(2006, 11, 31, 21, 36), new Date(2007, 0, 1));
trace("There are " + countdown.hours + "
hours and " + countdown.minutes + " minutes until the new year!");
</code>
*/
public static function getCountdownUntil(startDate:Date, endDate
:Date):Object {
var daysUntil:Number = ConversionUtil.millisecondsToDays
(DateUtil.getTimeBetween(startDate, endDate));
var hoursUntil:Number = ConversionUtil.daysToHours(days
Until % 1);
var minsUntil:Number = ConversionUtil.hoursToMinutes(h
oursUntil % 1);
var secsUntil:Number = ConversionUtil.minutesToSeconds
(minsUntil % 1);
var milliUntil:Number = ConversionUtil.secondsToMillise
conds(secsUntil % 1);
return {
days: int(daysUntil),
hours: int(hoursUntil),
minutes: int(minsUntil),
seconds: int(secsUntil),
milliseconds: int(milliUntil)};
}
/**
Determines the difference to coordinated universal time
(UTC) in seconds.
@param d: Date object to find the time zone offset of.
@return Returns the difference in seconds from UTC.
*/
public static function getDifferenceFromUTCInSeconds(d:Date):int
{
return ConversionUtil.minutesToSeconds(d.getTimezoneOffs
et());
}
/**
Determines the difference to coordinated universal time
(UTC) in hours.
@param d: Date object to find the time zone offset of.
@return Returns the difference in hours from UTC.
*/
public static function getDifferenceFromUTCInHours(d:Date):int {
return ConversionUtil.minutesToHours(d.getTimezoneOffset
());
}
/**
Formats the difference to coordinated undefined time (UT
C).
@param d: Date object to find the time zone offset of.
@param separator: The character(s) that separates the ho
urs from minutes.
@return Returns the formatted time difference from UTC.
*/
public static function getFormattedDifferenceFromUTC(d:Date, sep
arator:String = ""):String {
var pre:String = (-d.getTimezoneOffset() < 0) ? '-' : '+
';
return pre + NumberUtil.addLeadingZero(Math.floor(DateUt
il.getDifferenceFromUTCInHours(d))) + separator + NumberUtil.addLeadingZero(d.ge
tTimezoneOffset() % 60);
}
/**
Determines the time zone of the user from a Date object.
@param d: Date object to find the time zone of.
@return Returns the time zone abbreviation.
@example
<code>
trace(DateUtil.getTimezone(new Date()));
</code>
*/
public static function getTimezone(d:Date):String {
var timeZones:Array = new Array('IDLW', 'NT', 'HST', 'AK
ST', 'PST', 'MST', 'CST', 'EST', 'AST', 'ADT', 'AT', 'WAT', 'GMT', 'CET', 'EET',
'MSK', 'ZP4', 'ZP5', 'ZP6', 'WAST', 'WST', 'JST', 'AEST', 'AEDT', 'NZST');
var hour:uint = Math.round(12 + -(d.getTimezoneOff
set() / 60));
if (DateUtil.isDaylightSavings(d))
hour--;
return timeZones[hour];
}
/**
Determines if year is a leap year or a common year.
@param year: The full year.
@return Returns <code>true</code> if year is a leap year
; otherwise <code>false</code>.
@example
<code>
var myDate:Date = new Date(2000, 0, 1);
trace(DateUtil.isLeapYear(myDate.getFull
Year())); // Traces true
</code>
*/
public static function isLeapYear(year:Number):Boolean {
return DateUtil.getDaysInMonth(year, 1) == 29;
}
/**
Determines if or not the date is in daylight saving time
.
@param d: Date to find if it is during daylight savings
time.
@return Returns <code>true</code> if daylight savings ti
me; otherwise <code>false</code>.
*/
public static function isDaylightSavings(d:Date):Boolean {
var months:uint = 12;
var offset:uint = d.getTimezoneOffset();
var offsetCheck:Number;
while (months--) {
offsetCheck = (new Date(d.getFullYear(), months,
1)).getTimezoneOffset();
if (offsetCheck != offset)
return (offsetCheck > offset);
}
return false;
}
/**
Converts current time into Swatch internet time or beats
.
@param d: Date object to convert.
@return Returns time in beats (0 to 999).
*/
public static function getInternetTime(d:Date):Number {
var beats:uint = ((d.getUTCHours() + 1 + ConversionUtil.
minutesToHours(d.getUTCMinutes()) + ConversionUtil.secondsToHours(d.getUTCSecond
s())) / 0.024);
return (beats > 1000) ? beats - 1000 : beats;
}
/**
Gets the current day out of the total days in the year (
starting from 0).
@param d: Date object to find the current day of the yea
r from.
@return Returns the current day of the year (0-364 or 0-
365 on a leap year).
*/
public static function getDayOfTheYear(d:Date):uint {
var firstDay:Date = new Date(d.getFullYear(), 0, 1);
return (d.getTime() - firstDay.getTime()) / 86400000;
}
/**
Determines the week number of year, weeks start on Monda
ys.
@param d: Date object to find the current week number of
.
@return Returns the the week of the year the date falls
in.
*/
public static function getWeekOfTheYear(d:Date):uint {
var firstDay:Date = new Date(d.getFullYear(), 0, 1);
var dayOffset:uint = 9 - firstDay.getDay();
var firstMonday:Date = new Date(d.getFullYear(), 0, (day
Offset > 7) ? dayOffset - 7 : dayOffset);
var currentDay:Date = new Date(d.getFullYear(), d.getMo
nth(), d.getDate());
var weekNumber:uint = (ConversionUtil.millisecondsToDay
s(currentDay.getTime() - firstMonday.getTime()) / 7) + 1;
return (weekNumber == 0) ? DateUtil.getWeekOfTheYear(new
Date(d.getFullYear() - 1, 11, 31)) : weekNumber;
}
/**
Determines if two Dates are the same time.
@param first: First Date to compare to <code>second</cod
e>.
@param second: Second Date to compare to <code>first</co
de>.
@return Returns <code>true</code> if Dates are the same;
otherwise <code>false</code>.
*/
public static function equals(first:Date, second:Date):Boolean {
return first.valueOf() == second.valueOf();
}
}
}