JavaScript Date Format: API

DateFormat object

Constructors

DateFormat()

Constructs an empty DateFormat object, eg. pattern ””, locale Locale.DEFAULT. You'll have to call setStyle() or set the pattern property to something meaningfull if you want the DateFormat to make sense.

DateFormat(dateStyle, timeStyle, locale)

Constructs a DateFormat object with specified predefined patterns and specific locale.

ParameterTypeDescription
dateStyle, timeStyleNumberOptional Default is DateFormat.NONE. Specifies which predefined date/time formatting pattern to use. Possible values are DateFormat.NONE, DateFormat.SHORT, DateFormat.MEDIUM, DateFormat.LONG and DateFormat.FULL.
LocaleString or LocaleOptional Default is Locale.DEFAULT.

DateFormat(pattern)

Constructs a DateFormat object with the specified pattern string.

ParameterTypeDescription
patternStringSee the description of property pattern

Properties

pattern

Type: String

The current formatting pattern. It's syntax is almost equal to the formatting pattern in the java class --> java.text.SimpleDateFormat except that the lines starting with a '#' are not implemented, because they would require some advanced date calculations that are not provided by the JavaScript Date object. Maybe I'll implement them in some future version.

Symbol   Meaning                 Presentation        Example
------   -------                 ------------        -------
G        era designator          (Text)              AD
y        year                    (Number)            1996
M        month in year           (Text & Number)     July & 07
d        day in month            (Number)            10
h        hour in am/pm (1~12)    (Number)            12
H        hour in day (0~23)      (Number)            0
m        minute in hour          (Number)            30
s        second in minute        (Number)            55
S        millisecond             (Number)            978
E        day in week             (Text)              Tuesday
#D        day in year             (Number)            189
#F        day of week in month    (Number)            2 (2nd Wed in July)
#w        week in year            (Number)            27
#W        week in month           (Number)            2
a        am/pm marker            (Text)              PM
k        hour in day (1~24)      (Number)            24
K        hour in am/pm (0~11)    (Number)            0
z        time zone               (Text)              Pacific Standard Time
'        escape for text         (Delimiter)
''       single quote            (Literal)           '

The count of pattern letters determine the format.

* (Text): 4 or more pattern letters – use full form, < 4 letters – use short or abbreviated form if one exists. * (Number): The minimum number of digits. Shorter numbers are zero-padded to this amount. Year is handled specially; that is, if the count of 'y' is 2, the Year will be truncated to 2 digits. * (Text & Number): 3 or over, use text, otherwise use number.

Any characters in the pattern that are not in the ranges of 'a'..'z' and 'A'..'Z' will be treated as quoted text. For instance, characters like ':', '.', ' ', '#' and '@' will appear in the resulting time text even they are not embraced within single quotes.

locale

Type: Locale object

The current Locale object. Available Locales can be accessed through the Locale.availableLocales collection.

Methods

format(date)

This one does all the work, eg. it takes a JavaScript date as parameter and outputs a formatted string according to the current formatting pattern.

ParameterTypeDescription
dateDate, String or NumberRequired Obviously the date you want to format.

setStyle(dateStyle, timeStyle)

This one lets you specify one of the predefined formatting pattern styles. Useful in conjunction with the no-param constructor.

ParameterTypeDescription
dateStyle, timeStyleNumberRequired Specifies which predefined date/time formatting pattern to use. Possible values are DateFormat.NONE, DateFormat.SHORT, DateFormat.MEDIUM, DateFormat.LONG and DateFormat.FULL.

Locale Object

Constructors

Locale(name)

Constructs an empty Locale object with the given name.

ParameterTypeDescription
nameStringRequired The Locale's name. Must be unique. The newly created Locale object is stored in the Locale.availableLocales collection under this name.

Locale(name, parent)

Constructs a Locale object with the given name that inherits all it's properties from the specified parent Locale.

ParameterTypeDescription
nameStringRequired The Locale's name. Must be unique. The newly created Locale object is stored in the Locale.availableLocales collection under this name.
parentString or LocaleOptional Either an existing Locale's name or a Locale object. The newly created Locale object will have the same properties as the parent.

Properties

NameTypeDescription
nameStringThe Locale's name. The Locales object is accessible in the Locale.availableLocales collection under this name.
shortWeekdaysArrayContains the abbreviated weekday names, eg. Su, Mo.
longWeekdaysArrayContains the full weekday names, eg. Sunday, Monday.
shortMonthsArrayContains the abbreviated month names, eg. Jan, Feb.
longMonthsArrayContains the full month names, eg. January, February.
eraArrayContains the era names, eg. BC, AD.
amPmArrayContains the AM/PM makers, eg. AM, PM.
datePatternArrayContains the predefined date formatting patterns.
timePatternArrayContains the predefined time formatting patterns.

Methods

toString()

Returns the Locale's name.


Back to Date Format