Method Date.format
Format a date
-
Syntax
String Date.format(String format , [ Boolean UTC ])
Formats a date according to the format string provided. If UTC is not true then the local time is used. This function is modeled to mimic the PHP date function. -
Parameters
String format- The following characters are recognized in the input string
Day d Day of the month, 2 digits with leading zeros D A textual representation of a day, three letters j Day of the month without leading zeros l A full textual representation of the day of the week N ISO-8601 numeric representation of the day of the week S English ordinal suffix for the day of the month, 2 characters w Numeric representation of the day of the week (sunday=0) z The day of the year (starting from 0) N/A Week W ISO-8601 week number of year, weeks starting on Monday Month F A full textual representation of a month, such as January or March m Numeric representation of a month, with leading zeros M A short textual representation of a month, three letters n Numeric representation of a month, without leading zeros t Number of days in the given month Year L Whether it\'s a leap year 1 if it is a leap year, 0 otherwise Y A full numeric representation of a year, 4 digits y A two digit representation of a year Time a Lowercase Ante meridiem and Post meridiem (am or pm) A Uppercase Ante meridiem and Post meridiem (AM or PM) 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 s Seconds, with leading zeros 00 through 59 u Milliseconds Timezone e Timezone identifier (Examples: UTC, GMT, Atlantic/Azores) N/A I Whether or not the date is in daylight saving time 1 if Daylight Saving Time, 0 otherwise. O Difference to Greenwich time (GMT) in hours P Difference to Greenwich time (GMT) with colon between hours and minutes T Timezone abbreviation:n/a\n Z Timezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive. Full date/time c ISO 8601 date r RFC 2822 formatted date U Seconds since the Unix Epoch Boolean UTC(Optional)- If true UTC is used; otherwise the local time is used.
- Return value A string representation of the date as specified by format.
-
Examples
- To format a date in a nice human readable manner
// Create date - 1/2/2010 at half past midday var d = new Date(2010,1,1,12,30); // Output '12:30 on 1st of February 2010' d.format('H:i') + ' on ' + d.format('jS') + ' of ' + d.format('F Y');
-
Download
Source
Unless otherwise noted all code in the JSLab Standard Library is licensed as GPLv3. See http://www.gnu.org/licenses/gpl.html for the entire license.
This function has dependencies and will not work stand-alone. Use the download button above instead of copy/paste.
