JavaScript

DatetoFormat Method

Syntax

Date.toFormat([format])

Arguments

formatstring

The format to show the date in. If no format is specified the default format in A5.d.date.format will be used.

Returns

datestring

The formatted date.

Description

Extension to the native date variable to create a formatted string.

Discussion

By passing in a date format this method will return a formatted date string.

A number of values can be used in the format. See the list below.

"y" or "yy" can be used to show the year in two digits (for instance 99 or 03 for 1999 and 2003). "yyyy" will show the full year (for instance 2018).

"M" will show the month as a single digit. "MM" will show the month as two digits digit (for instance "01" instead of "1" for January). "MON" will show the month in the short name stored in the A5.d.date.monthsShort array in upper-case. "mon" will show the short month name in lower-case. "Mon" will show the short month name in title-case. "MONTH" will show the month in the full name stored in the A5.d.date.months array in upper-case. "month" will show the full month name in lower-case. "Month" will show the full month name in title-case.

"d" will show the day of the month in a single digit. "dd" will show the day of the month in two digits digit. "x" will show the day with "st", "rd" or "th". "X" will show the day with "ST", "RD" or "TH".

"W" or "WD" will show the weekday in the short name stored in the A5.d.date.daysShort array in upper-case. "w" or "wd" will show the short weekday name in lower-case. "Wd" will show the short weekday name in title-case. "WEEKDAY" will show the weekday in the full name stored in the A5.d.date.days array in upper-case. "weekday" will show the full weekday name in lower-case. "Weekday" will show the full weekday name in title-case.

"h" will show the date time hours. "m" will show the date time minutes. "s" will show the date time seconds. "0" when placed before a "h", "m" or "s" will pad the value with zeros. "1" will show the date time milliseconds to a tenth of a second. "2" will show the date time milliseconds to a hundredth of a second. "3" will show the date time milliseconds to a thousandth of a second. "hh", "mm" and "ss" can also be used to pad hours, minutes ans seconds with zeros. "A" will convert the hours from 24 to 12 and place the meridian in as a single character in upper-case. "am" will convert the hours from 24 to 12 and place the meridian in as two characters in lower-case. "AM" will convert the hours from 24 to 12 and place the meridian in as two characters in upper-case.

"\\" can be used in front of any of the above placeholders to escape them.

Example

var d = new Date(2018,3,21,14,5,0);
var dStr = d.toFormat('Wd t\\he x Mon yyyy h:0m am');
// dStr = 'Sat the 21st Apr 2018 2:05 pm'