Jel.Date

Utility Methods for manipulating JavaScript’s built-in Date class

Summary
Utility Methods for manipulating JavaScript’s built-in Date class
a hash collection of common formatting string constants to be used with Jel.Date.format and Jel.Date.parse
a hash collection of common date format string constants as usually expressed by humans, with each constant being equivalent to those in Jel.Date.FORMAT
parses a date string into a JavaScript Date object assuming a specified date format.
formats the given date as a string using the date format in format.
converts a date string from one format to another.
gets a Date object representing the current date, with an optional format string.
gets the number of days for a given month and year.
checks if a given year is a leap year
checks if a given day, month, and year combination is a valid date in the Gregorian calendar
gets the ante/post meridiem (am or pm) for a given hour
gets the twelve hour value for the given hour
gets the English ordinal suffix for a given day (st, nd, rd, th)
gets the full 4-digit year for this Date object
gets the day of the year for the given date
gets the Swatch internet time for the given date object

Properties

FORMAT

a hash collection of common formatting string constants to be used with Jel.Date.format and Jel.Date.parse

Examples

Jel.Date.format(Jel.Date.parse("23/02/2006", Jel.Date.FORMAT.UK), Jel.Date.FORMAT.US);
// "02/23/2006"

Jel.Date.format(Jel.Date.parse("23/02/2006 11:50 PM", Jel.Date.FORMAT.UK_12), Jel.Date.FORMAT.UTC);
// "2006-23-02 23:50:00"

Available constants

T_12:                     'g:i A'
T_24: 'G:i'
T_MILITARY: 'Gi'
UK: 'd/m/Y'
US: 'm/d/Y'
UK_12: 'd/m/Y g:i A'
US_12: 'm/d/Y g:i A'
UK_24: 'd/m/Y G:i'
US_24: 'm/d/Y G:i'
UK_12_SHORT: 'd/m/Y g A'
US_12_SHORT: 'm/d/Y g A'
UK_24_SHORT: 'd/m/Y G'
US_24_SHORT: 'm/d/Y G'
UK_12_LONG: 'd/m/Y g:i:s A'
US_12_LONG: 'm/d/Y g:i:s A'
UK_24_LONG: 'd/m/Y G:i:s'
US_24_LONG: 'm/d/Y G:i:s'
UTC: 'Y-m-d G:i:s'
UTC_T: 'Y-m-dTG:i:s'
UTC_Y: 'Y'
UTC_YM: 'Y-m'
UTC_YMD: 'Y-m-d'
UTC_YMDHM: 'Y-m-d g:i'
UTC_YMDHMS: 'Y-m-d g:i:s'
SHORT_MONTH: 'd M Y',
SHORT_MONTH_12: 'd M Y g:i A'
SHORT_MONTH_24: 'd M Y G:i'
SHORT_MONTH_PHRASE: 'jS M Y'
SHORT_MONTH_PHRASE_12: 'jS M Y g:i A'
SHORT_MONTH_PHRASE_24: 'jS M Y G:i'

See also

http://www.w3.org/TR/NOTE-datetime has more information about the UTC (Coordinated Universal Time) standard formats

HUMAN_FORMAT

a hash collection of common date format string constants as usually expressed by humans, with each constant being equivalent to those in Jel.Date.FORMAT

Available constants

T_12:                     'h:mm AM/PM'
T_24: 'hh:mm (24 hour)'
T_MILITARY: 'hmm (military time)'
UK: 'dd/mm/yyyy'
US: 'mm/dd/yyyy'
UK_12: 'dd/mm/yyyy h:mm AM/PM'
US_12: 'mm/dd/yyyy h:mm AM/PM'
UK_24: 'dd/mm/yyyy hh:mm (24 hour)'
US_24: 'mm/dd/yyyy hh:mm (24 hour)'
UK_12_SHORT: 'dd/mm/yyyy h AM/PM'
US_12_SHORT: 'mm/dd/yyyy h AM/PM'
UK_24_SHORT: 'dd/mm/yyyy h (24 hour)'
US_24_SHORT: 'mm/dd/yyyy h (24 hour)'
UK_12_LONG: 'dd/mm/yyyy h:mm:ss AM/PM'
US_12_LONG: 'mm/dd/yyyy h:mm:ss AM/PM'
UK_24_LONG: 'dd/mm/yyyy h:mm:ss (24 hour)'
US_24_LONG: 'mm/dd/yyyy h:mm:ss (24 hour)'
UTC: 'yyyy-mm-dd hh:mm:ss (24 hour)'
UTC: 'yyyy-mm-dd hh:mm:ss (24 hour)'
UTC_T: 'yyyy-mm-ssThh:mm:ss (24 hour)'
UTC_Y: 'yyyy'
UTC_YM: 'yyyy-mm'
UTC_YMD: 'yyyy-mm-dd'
UTC_YMDHM: 'yyyy-mm-dd hh:mm (24 hour)'
UTC_YMDHMS: 'yyyy-mm-dd hh:mm:ss (24 hour)'
SHORT_MONTH: "dd mmm yyyy"
SHORT_MONTH_12: "dd mmm yyyy hh:mm AM/PM"
SHORT_MONTH_24: "dd mmm yyyy hh:mm (24 hour)"
SHORT_MONTH_PHRASE: "d(th) mmm yyyy"
SHORT_MONTH_PHRASE_12: "d(th) mmm yyyy hh:mm AM/PM"
SHORT_MONTH_PHRASE_24: "d(th) mmm yyyy hh:mm (24 hour)"

Functions

parse

Jel.Date.parse = function(str,
format)

parses a date string into a JavaScript Date object assuming a specified date format.

Arguments

strstring, the string to parse.
formatstring, describes the format of the input string.  Note that this can contain any arbritrary characters except the reserved formatting characters listed below (these characters are based on formatting characters used for in the PHP date function)

Example

Jel.Date.parse("23/04/2006", "d/m/Y");
// [Date object with day = 23, month = 3, year = 2006]

Jel.Date.parse("23 Feb 2006", "d/m/Y");
// false, not in expected format

Returns

Date objectif the date string is in the specified format, and it is a real date in the Gregorian calendar
falseotherwise

Formatting characters

d  Day of the month, 2 digits with leading zeros:                       01 - 31
D A textual representation of a day, three letters: Mon - Sun
j Day of the month without leading zeros: 1 - 31
l A full textual representation of the day of the week: Sunday - Saturday
S English ordinal suffix for the day of the month, 2 characters: st, nd, rd or th.
F A full textual representation of a month, such as January or March: January - December
m Numeric representation of a month, with leading zeros: 01 - 12
M A short textual representation of a month, three letters: Jan - Dec
n Numeric representation of a month, without leading zeros: 1 - 12
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 / pm
A Uppercase Ante meridiem and Post meridiem: AM / PM
g 12-hour format of an hour without leading zeros: 1 - 12
G 24-hour format of an hour without leading zeros: 0 - 23
h 12-hour format of an hour with leading zeros: 01 - 12
H 24-hour format of an hour with leading zeros: 00 - 23
i Minutes with leading zeros: 00 - 59
s Seconds, with leading zeros: 00 - 59

See also

Jel.Date.format, Jel.Date.FORMAT

format

Jel.Date.format = function(date,
format)

formats the given date as a string using the date format in format.

Arguments

dateDate, the date to format
formatstring, describes the format of the output string using the reserved formatting characters listed below.  Any other characters present will simply appear in the output string in the same place.

Returns

stringcontaining the formatted date

Example

Jel.Date.format(new Date(2007, 2, 2), "d/m/Y"); // 02/03/2007
Jel.Date.format(new Date(2007, 2, 2), "jS M Y"); // 2nd March 2007

Formatting characters (based on formatting characters used in the PHP date function)

d   Day of the month, 2 digits with leading zeros:                      01 - 31
D A textual representation of a day, three letters: Mon - Sun
j Day of the month without leading zeros: 1 - 31
l A full textual representation of the day of the week: Sunday - Saturday
N ISO-8601 numeric representation of the day of the week: 1 (for Monday) - 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) - 6 (for Saturday)
z The day of the year (starting from 0): 0 - 365
F A full textual representation of a month: January - December
m Numeric representation of a month, with leading zeros: 01 - 12
M A short textual representation of a month, three letters: Jan - Dec
n Numeric representation of a month, without leading zeros: 1 - 12
t Number of days in the given month: 28 - 31
L Whether it's a leap year: 1 (leap year), 0 (otherwise).
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 / pm
A Uppercase Ante meridiem and Post meridiem: AM / PM
B Swatch Internet time: 000 through 999
g 12-hour format of an hour without leading zeros: 1 - 12
G 24-hour format of an hour without leading zeros: 0 - 23
h 12-hour format of an hour with leading zeros: 01 - 12
H 24-hour format of an hour with leading zeros: 00 - 23
i Minutes with leading zeros: 00 - 59
s Seconds, with leading zeros: 00 - 59
c ISO 8601 date: Example: 2004-02-12T15:19:21+00:00
r RFC 2822 formatted date: Example - Thu, 21 Dec 2000 16:01:07 +0200

See also: Jel.Date.parse, Jel.Date.FORMAT

convert

Jel.Date.convert = function(str,
fromFormat,
toFormat)

converts a date string from one format to another.  Essentially performs Jel.Date.parse on a given string, followed by Jel.Date.format on the return date object

Arguments

strstring, the original date string to parse
fromFormatthe expected format of the date in str (refer to Jel.Date.parse for valid formatter characters)
toFormatthe desired output format (refer to Jel.Date.format for valid formatter characters)

Returns

stringthe formatted date, if both the original date string was in the expected format, and the parsed date was a real date
falseotherwise

Examples

Jel.Date.convert("28 Feb 2006 2PM", "j M Y gA", Jel.Date.FORMAT.UK_12);
// "28/02/2006 2:00 PM"

Jel.Date.convert("30/04/2007 10:00 PM", Jel.Date.FORMAT.US_12, "js M Y, gA");
// "30th April 2007, 10PM"

Jel.Date.convert("31/02/2007 10:00 PM", Jel.Date.FORMAT.US_12, "js M Y, gA");
// false, form format correct, but not a real date

Jel.Date.convert("31st March 2006", "d/m/Y", "js M Y, gA");
// false, from format is incorrect

now

Jel.Date.now = function(format)

gets a Date object representing the current date, with an optional format string.

Arguments

formatString (optional), if provided, will return a string of the current date in this format , using Jel.Date.format

Returns

Dateif format is not specified
Stringotherwise

daysInMonth

Jel.Date.daysInMonth = function(month,
year)

gets the number of days for a given month and year.

Arguments

monthinteger, the month of the year 0-11 (not 1-12, as standard in JavaScript)
yearinteger, the 4 digit year

Returns

integer

Examples

Jel.Date.daysInMonth(2, 2006); // 31

isLeapYear

Jel.Date.isLeapYear = function(year)

checks if a given year is a leap year

Arguments

yearinteger, the 4 digit year

Returns

trueif the year is a leap year
falseotherwise

Examples

Jel.Date.isLeapYear(2004); // true
Jel.Date.isLeapYear(2006); // false

validDayMonthYear

Jel.Date.validDayMonthYear = function(day,
month,
year)

checks if a given day, month, and year combination is a valid date in the Gregorian calendar

Arguments

monthinteger, the month of the year 0-11 (not 1-12, as standard in JavaScript)
yearinteger, the 4 digit year

Returns

trueif the combination is a valid day, month, and year combination
falseotherwise

meridiem

Jel.Date.meridiem = function(hour)

gets the ante/post meridiem (am or pm) for a given hour

Arguments

hourinteger, the hour of the day in 24-hour time (0-23)

Returns

string”am” or “pm”

Examples

Jel.Date.meridiem(0); // "am"
Jel.Date.meridiem(5); // "am"
Jel.Date.meridiem(12); // "pm"
Jel.Date.meridiem(17); // "pm"

twelveHour

Jel.Date.twelveHour = function(hour)

gets the twelve hour value for the given hour

Arguments

hourinteger an hour from 0-23 (24 hour time)

Examples

Jel.Date.twelveHour(0); // 12
Jel.Date.twelveHour(4); // 4
Jel.Date.twelveHour(12); // 12
Jel.Date.twelveHour(13); // 1
Jel.Date.twelveHour(18); // 6

ordinalSuffix

Jel.Date.ordinalSuffix = function(day)

gets the English ordinal suffix for a given day (st, nd, rd, th)

Arguments

dayInteger

Returns

string

See also

Jel.Number.ordinalSuffix

Example

Jel.Date.ordinalSuffix(5) // th

fullYear

Jel.Date.fullYear = function(date)

gets the full 4-digit year for this Date object

Arguments

dateDate object

Returns: integer

Example

Jel.Date.fullYear(new Date(2007, 6, 30));    // 2007

Credit

Thanks to Peter-Paul Koch of http://www.quirksmode.org/ for the basis of this code

dayOfYear

Jel.Date.dayOfYear = function(date)

gets the day of the year for the given date

Arguments

dateDate object

Returns: integer

Example

Jel.Date.dayOfYear(new Date(2007, 6, 30));    // 211

internetBeat

Jel.Date.internetBeat = function(date)

gets the Swatch internet time for the given date object

Arguments

dateDate object

Returns: integer

Example

Jel.Date.internetBeat(new Date(2007, 06, 30, 20, 40, 0));    // 486

Credit

Thanks to Peter-Paul Koch of http://www.quirksmode.org/ for this code

Jel.Date.format = function(date,
format)
formats the given date as a string using the date format in format.
Jel.Date.parse = function(str,
format)
parses a date string into a JavaScript Date object assuming a specified date format.
a hash collection of common formatting string constants to be used with Jel.Date.format and Jel.Date.parse
Jel.Date.convert = function(str,
fromFormat,
toFormat)
converts a date string from one format to another.
Jel.Date.now = function(format)
gets a Date object representing the current date, with an optional format string.
Jel.Date.daysInMonth = function(month,
year)
gets the number of days for a given month and year.
Jel.Date.isLeapYear = function(year)
checks if a given year is a leap year
Jel.Date.validDayMonthYear = function(day,
month,
year)
checks if a given day, month, and year combination is a valid date in the Gregorian calendar
Jel.Date.meridiem = function(hour)
gets the ante/post meridiem (am or pm) for a given hour
Jel.Date.twelveHour = function(hour)
gets the twelve hour value for the given hour
Jel.Date.ordinalSuffix = function(day)
gets the English ordinal suffix for a given day (st, nd, rd, th)
Jel.Date.fullYear = function(date)
gets the full 4-digit year for this Date object
Jel.Date.dayOfYear = function(date)
gets the day of the year for the given date
Jel.Date.internetBeat = function(date)
gets the Swatch internet time for the given date object
ordinalSuffix: function(number)
gets the English ordinal suffix for a given number (st, nd, rd, th)