DateUtil
Kind of class: | class |
---|---|
Inherits from: | none |
Version: | 07/08/07 |
Author: | Aaron Clinger, Shane McCartney |
Classpath: | org.casalib.util.DateUtil |
File last modified: | Monday, 01 December 2008, 13:34:40 |
Provides utility functions for formatting and manipulating
Date
objects.Summary
Class methods
- formatDate (dateToFormat:Date, formatString:String) : String
- Formats a Date object for display.
- iso8601ToDate (iso8601:String) : Date
- Converts W3C ISO 8601 date strings into a Date object.
- getMonthAsString (month:Number) : String
- Converts the month number into the full month name.
- getMonthAbbrAsString (month:Number) : String
- Converts the month number into the month abbreviation.
- getDayAsString (day:Number) : String
- Converts the day of the week number into the full day name.
- getDayAbbrAsString (day:Number) : String
- Converts the day of the week number into the day abbreviation.
- getDaysInMonth (year:Number, month:Number) : Number
- Finds the number of days in the given month.
- getMeridiem (hours:Number) : String
- Determines if time is Ante meridiem or Post meridiem.
- getTimeBetween (startDate:Date, endDate:Date) : Number
- Determines the difference between two dates.
- getCountdownUntil (startDate:Date, endDate:Date) : Object
- Determines the time remaining until a certain date.
- getDifferenceFromUTCInSeconds (d:Date) : Number
- Determines the difference to coordinated universal time (UTC) in seconds.
- getDifferenceFromUTCInHours (d:Date) : Number
- Determines the difference to coordinated universal time (UTC) in hours.
- getFormattedDifferenceFromUTC (d:Date, separator:String) : String
- Formats the difference to coordinated undefined time (UTC).
- getTimezone (d:Date) : String
- Determines the time zone of the user from a Date object.
- isLeapYear (year:Number) : Boolean
- Determines if year is a leap year or a common year.
- isDaylightSavings (d:Date) : Boolean
- Determines whether or not the date is in daylight saving time.
- getInternetTime (d:Date) : Number
- Converts current time into Swatch internet time or beats.
- getDayOfTheYear (d:Date) : Number
- Gets the current day out of the total days in the year (starting from 0).
- getWeekOfTheYear (d:Date) : Number
- Determines the week number of year, weeks start on Mondays.
Class methods
formatDate
static function formatDate (
dateToFormat:Date,
formatString:String) : String
Formats a Date object for display. Acts almost identically to the PHP
date
function.Format character | Description | Example returned values |
---|---|---|
d | Day of the month, 2 digits with leading zeros. | 01 to 31 |
D | A textual representation of a day, three letters. | Mon through Sun |
j | Day of the month without leading zeros. | 1 to 31 |
l | A full textual representation of the day of the week. | Sunday through Saturday |
N | ISO-8601 numeric representation of the day of the week. | 1 (for Monday) through 7 (for Sunday) |
S | English ordinal suffix for the day of the month, 2 characters. | st, nd, rd or th |
w | Numeric representation of the day of the week. | 0 (for Sunday) through 6 (for Saturday) |
z | The day of the year (starting from 0). | 0 through 365 |
W | ISO-8601 week number of year, weeks starting on Monday. | Example: 42 (the 42nd week in the year) |
F | A full textual representation of a month, such as January or March. | January through December |
m | Numeric representation of a month, with leading zeros. | 01 through 12 |
M | A short textual representation of a month, three letters. | Jan through Dec |
n | Numeric representation of a month, without leading zeros. | 1 through 12 |
t | Number of days in the given month. | 28 through 31 |
L | Whether it's a leap year. | 1 if it is a leap year, 0 otherwise |
o or Y | A full numeric representation of a year, 4 digits. | Examples: 1999 or 2003 |
y | A two digit representation of a year. | Examples: 99 or 03 |
a | Lowercase Ante meridiem and Post meridiem. | am or pm |
A | Uppercase Ante meridiem and Post meridiem. | AM or PM |
B | Swatch Internet time. | 000 through 999 |
g | 12-hour format of an hour without leading zeros. | 1 through 12 |
G | 24-hour format of an hour without leading zeros. | 0 through 23 |
h | 12-hour format of an hour with leading zeros. | 01 through 12 |
H | 24-hour format of an hour with leading zeros. | 00 through 23 |
i | Minutes with leading zeros. | 00 to 59 |
s | Seconds, with leading zeros. | 00 through 59 |
I | Whether or not the date is in daylight saving time. | 1 if Daylight Saving Time, 0 otherwise |
O | Difference to coordinated universal time (UTC) in hours. | Example: +0200 |
P | Difference to Greenwich time (GMT/UTC) in hours with colon between hours and minutes. | Example: +02:00 |
e or T | Timezone abbreviation. | Examples: EST, MDT |
Z | Timezone offset in seconds. | -43200 through 50400 |
c | ISO 8601 date. | 2004-02-12T15:19:21+00:00 |
r | RFC 2822 formatted date. | Example: Thu, 21 Dec 2000 16:01:07 +0200 |
U | Seconds since the Unix Epoch. | Example: 1171479314 |
Parameters:
dateToFormat:
The Date object you wish to format.
formatString:
The format of the outputted date string. See the format characters options above.
Usage note:
- You can prevent a recognized character in the format string from being expanded by escaping it with a preceding
^
.
Example:
-
trace(DateUtil.formatDate(new Date(), "l ^t^h^e dS ^of F Y h:i:s A"));
getCountdownUntil
static function getCountdownUntil (
startDate:Date,
endDate:Date) : Object
Determines the time remaining until a certain date.
Parameters:
startDate:
The starting date.
endDate :
The ending date.
Returns:
- Returns an Object with the properties
days
,hours
,minutes
,seconds
andmilliseconds
defined as numbers.
Example:
-
var countdown:Object = DateUtil.getCountdownUntil(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!");
getDayAbbrAsString
static function getDayAbbrAsString (
day:Number) : String
Converts the day of the week number into the day abbreviation.
Parameters:
day:
An integer representing the day of the week (0 for Sunday, 1 for Monday, and so on).
Returns:
- Returns a textual representation of a day, three letters.
Example:
-
var myDate:Date = new Date(2000, 0, 1); trace(DateUtil.getDayAbbrAsString(myDate.getDay())); // Traces Sat
getDayAsString
static function getDayAsString (
day:Number) : String
Converts the day of the week number into the full day name.
Parameters:
day:
An integer representing the day of the week (0 for Sunday, 1 for Monday, and so on).
Returns:
- Returns a full textual representation of the day of the week.
Example:
-
var myDate:Date = new Date(2000, 0, 1); trace(DateUtil.getDayAsString(myDate.getDay())); // Traces Saturday
getDayOfTheYear
static function getDayOfTheYear (
d:Date) : Number
Gets the current day out of the total days in the year (starting from 0).
Parameters:
d:
Date object to find the current day of the year from.
Returns:
- Returns the current day of the year (0-364 or 0-365 on a leap year).
getDaysInMonth
static function getDaysInMonth (
year:Number,
month:Number) : Number
Finds the number of days in the given month.
Parameters:
year :
The full year.
month:
The month number (0 for January, 1 for February, and so on).
Returns:
- The number of days in the month; 28 through 31.
Example:
-
var myDate:Date = new Date(2000, 0, 1); trace(DateUtil.getDaysInMonth(myDate.getFullYear(), myDate.getMonth())); // Traces 31
getDifferenceFromUTCInHours
static function getDifferenceFromUTCInHours (
d:Date) : Number
Determines the difference to coordinated universal time (UTC) in hours.
Parameters:
d:
Date object to find the time zone offset of.
Returns:
- Returns the difference in hours from UTC.
getDifferenceFromUTCInSeconds
static function getDifferenceFromUTCInSeconds (
d:Date) : Number
Determines the difference to coordinated universal time (UTC) in seconds.
Parameters:
d:
Date object to find the time zone offset of.
Returns:
- Returns the difference in seconds from UTC.
getFormattedDifferenceFromUTC
static function getFormattedDifferenceFromUTC (
d:Date,
separator:String) : String
Formats the difference to coordinated undefined time (UTC).
Parameters:
d :
Date object to find the time zone offset of.
separator:
[optional] The character(s) that separates the hours from minutes; defaults to no separator/
""
.Returns:
- Returns the formatted time difference from UTC.
getInternetTime
static function getInternetTime (
d:Date) : Number
Converts current time into Swatch internet time or beats.
Parameters:
d:
Date object to convert.
Returns:
- Returns time in beats (0 to 999).
getMeridiem
static function getMeridiem (
hours:Number) : String
Determines if time is Ante meridiem or Post meridiem.
Parameters:
hours:
The hour to find the meridiem of (an integer from 0 to 23).
Returns:
- Returns either
"AM"
or"PM"
Example:
-
trace(DateUtil.getMeridiem(17)); // Traces PM
getMonthAbbrAsString
static function getMonthAbbrAsString (
month:Number) : String
Converts the month number into the month abbreviation.
Parameters:
month:
The month number (0 for January, 1 for February, and so on).
Returns:
- Returns a short textual representation of a month, three letters.
Example:
-
var myDate:Date = new Date(2000, 0, 1); trace(DateUtil.getMonthAbbrAsString(myDate.getMonth())); // Traces Jan
getMonthAsString
static function getMonthAsString (
month:Number) : String
Converts the month number into the full month name.
Parameters:
month:
The month number (0 for January, 1 for February, and so on).
Returns:
- Returns a full textual representation of a month, such as January or March.
Example:
-
var myDate:Date = new Date(2000, 0, 1); trace(DateUtil.getMonthAsString(myDate.getMonth())); // Traces January
getTimeBetween
static function getTimeBetween (
startDate:Date,
endDate:Date) : Number
Determines the difference between two dates.
Parameters:
startDate:
The starting date.
endDate :
The ending date.
Returns:
- Returns the difference in milliseconds between the two dates.
Example:
-
trace(ConversionUtil.millisecondsToDays(DateUtil.getTimeBetween(new Date(2007, 0, 1), new Date(2007, 0, 11)))); // Traces 10
getTimezone
static function getTimezone (
d:Date) : String
Determines the time zone of the user from a Date object.
Parameters:
d:
Date object to find the time zone of.
Returns:
- Returns the time zone abbreviation.
Example:
-
trace(DateUtil.getTimezone(new Date()));
getWeekOfTheYear
static function getWeekOfTheYear (
d:Date) : Number
Determines the week number of year, weeks start on Mondays.
Parameters:
d:
Date object to find the current week number of.
Returns:
- Returns the the week of the year the date falls in.
isDaylightSavings
static function isDaylightSavings (
d:Date) : Boolean
Determines whether or not the date is in daylight saving time.
Parameters:
d:
Date to find if it's during daylight savings time.
Returns:
- Returns
true
if daylight savings time; otherwisefalse
.
isLeapYear
static function isLeapYear (
year:Number) : Boolean
Determines if year is a leap year or a common year.
Parameters:
year:
The full year.
Returns:
- Returns
true
if year is a leap year; otherwisefalse
.
Example:
-
var myDate:Date = new Date(2000, 0, 1); trace(DateUtil.isLeapYear(myDate.getFullYear())); // Traces true
iso8601ToDate
static function iso8601ToDate (
iso8601:String) : Date
Converts W3C ISO 8601 date strings into a Date object.
Parameters:
iso8601:
A valid ISO 8601 formatted string.
Returns:
- Returns a Date object of the specified date and time of the ISO 8601 string in universal time.
See also:
Example:
-
trace(DateUtil.iso8601ToDate("1994-11-05T08:15:30-05:00").toString());