strftime()

November 10, 2022 ยท View on GitHub

A JavaScript port of strftime(), a function to format the date and time.

Supported conversion specifications:

SequenceDescriptionExample
%aAbbreviated name of the day of the week.Fri
%AFull name of the day of the week.Friday
%bAbbreviated month name.Apr
%BFull month name.April
%cUTC date and time representation for the current locale.Sat 02 Apr 2022 00:15:00 GMT
%CCentury number (year/100) as a 2-digit integer.20
%dDay of the month as a decimal number (range 01 to 31).01
%eDay of the month as a decimal number (range 1 to 31).1
%FISO 8601 date format (equivalent to %Y-%m-%d).2022-04-01
%gLike %G, but without century, that is, with a 2-digit year.22
%GISO 8601 week-based year with century as a decimal number. The 4-digit year corresponds to the ISO week number (see %V). This has the same format and value as %Y, except that if the ISO week number belongs to the previous or next year, that year is used instead.2022
%HHour as a decimal number using a 24-hour clock (range 00 to 23). See also %k.17
%IHour as a decimal number using a 12-hour clock (range 01 to 12). See also %l.05
%jDay of the year as a decimal number (range 001 to 366).091
%kHour as a decimal number using a 24-hour clock (range 0 to 23). See also %H.17
%lHour as a decimal number using a 12-hour clock (range 1 to 12). See also %I.5
%mMonth as a decimal number (range 01 to 12).04
%nMonth as a decimal number (range 1 to 12).4
%MMinute as a decimal number (range 00 to 59).15
%pEither "AM" or "PM" according to the given time value. Noon is treated as "PM" and midnight as "AM".PM
%PLike %p but in lowercase ("am" or "pm").pm
%sNumber of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).1648858500
%SSecond as a decimal number (range 00 to 59).00
%uDay of the week as a decimal (range 1 to 7), Monday being 1. See also %w.5
%VISO 8601 week number of the current year as a decimal number (range 01 to 53), where week 1 is the first week that has at least 4 days in the new year (that is, the first Thursday).13
%wDay of the week as a decimal (range 0 to 6), Sunday being 0. See also %u.5
%xPreferred date representation for the current locale without the time.4/1/2022
%XPreferred time representation for the current locale without the date.5:15:00 PM
%yYear as a decimal number without a century (range 00 to 99).22
%YYear as a decimal number including the century.2022
%zThe +hhmm or -hhmm numeric timezone (that is, the hour and minute offset from UTC).-0800
%ZTimezone name.Pacific Standard Time
%ZsTimezone short name (abbreviation).PST

Compatibility notes:

  • %c - formatted string is slightly different
  • %D - not implemented (use %m/%d/%y or %d/%m/%y)
  • %e - space is not added
  • %E - not implemented
  • %h - not implemented (use %b)
  • %k - space is not added
  • %n - like %m, but no leading zero (use \n for newline)
  • %O - not implemented
  • %r - not implemented (use %I:%M:%S %p)
  • %R - not implemented (use %H:%M)
  • %t - not implemented (use \t)
  • %T - not implemented (use %H:%M:%S)
  • %U - not implemented
  • %W - not implemented
  • %+ - not implemented
  • %% - not implemented (use %)

Sample usage:

// Returns "15-09-2016 16:20"
strftime('%d-%m-%Y %H:%M');

// You can optionally pass it a Date object
// Returns "01-01-2016 21:30"
strftime('%d-%m-%Y %H:%M', new Date('Jan 1, 2016 9:30 PM'));