Javascript Date Format

This adds functions to a Date object.
UPDATED 5/11/14

  • Initial Release

If you find any bugs please email me at [email protected]

Thanks

Download from Github

Usage:

Add format method to Date object in javascript to allow string formatting

adds the following methods to a date object:

getMonthName()
Gets the month name. (eg. January)

getMonthNameShort()
Gets the abbreviated month name. (eg. Jan)

getDayName()
Gets the day of the week name. (eg. Sunday)

getDayNameShort()
Gets the abbreviated day of the week name. (eg. Sun)

getDateSuffix()
Gets the date suffix for the day of the month. (eg. th)

getLocale()
Gets the locale for the month and day names. The default is "en".

setLocale(locale)
Sets the locale for the month and day names.

format(format_string)
Gets a string representation of the date object based on the format_string. (eg. "DDDD, MMMM DS h:mm TT" = Wednesday, January 1st 12:00 AM)

Demo:

Options

  • YYYY: 4 digit year =
  • YY: 2 digit year =
  • MMMM: Month name =
  • MMM: Month name abbreviation =
  • MM: Month 01-12 =
  • M: Month 1-12 =
  • DDDD: Day of the week name =
  • DDD: Day of the week name abbreviation =
  • DD: Day of the month 01-31 =
  • D: Day of the month 1-31 =
  • S: Day of the month suffix (st|nd|rd|th) =
  • HH: Hours 00-23 =
  • H: Hours 0-23 =
  • hh: Hours 01-12 =
  • h: Hours 1-12 =
  • mm: Minutes 00-59 =
  • m: Minutes 0-59 =
  • ss: Seconds 00-59 =
  • s: Seconds 0-59 =
  • f: Tenths of a second =
  • ff: Hundredths of a second =
  • fff: Thousandths of a second =
  • tt: am/pm =
  • TT: AM/PM =
  • zzzz: timezone =
  • zzz: timezone =
  • zz: timezone =
  • z: timezone =
  • ": Start of a literal string. Copies characters between quotes to output or copies rest of string if no closing quote. YYYY"YYYY =
  • ': Start of a literal string. Copies characters between quotes to output or copies rest of string if no closing quote. MMM'MMM =
  • \: Copies next character to output D\D =
  • Any other character will be copied to output /.* =

Try it