format

Source:

Date and time formatting

Members

(static) FORMAT_TOKENS

Source:

Functions for formatting Date objects

This object maps format tokens to formatting functions. This object is used by strftime() to perform the formatting. It can also be manipulated to modify and/or enhance the way stftime() formats date and time.

To add a new token, we first decide on the actual token we want to use, or whther we want to override an existing one. Then we assign a format function to that key.

The format functions take two arguments, the Date object to format, and the configuration object.

Example
format.FORMAT_TOKENS['%R'] = function romanYear(dt, conf) {
  // Do something with `dt` and/or `conf` and return a string...
};

Methods

(static) strftime(dt, formatString, configopt)

Source:

Format a Date object according to a format string

The formatting uses strftime-compatible syntax with follwing tokens:

  • %a - short week day name (e.g. 'Sun', 'Mon'...).
  • %A - long week day name (e.g., 'Sunday', 'Monday'...).
  • %b - short month name (e.g., 'Jan', 'Feb'...).
  • %B - full month name (e.g., 'January', 'February'...).
  • %c - locale-formatted date and time (platform-dependent).
  • %d - zero-padded date (e.g, 02, 31...).
  • %D - non-zero-padded date (e.g., 2, 31...).
  • %f - zero-padded decimal seconds (e.g., 04.23, 23.50).
  • %H - zero-padded hour in 24-hour format (e.g., 8, 13, 0...).
  • %i - non-zero-padded hour in 12-hour format (e.g., 8, 1, 12...).
  • %I - zero-padded hour in 12-hour format (e.g., 08, 01, 12...).
  • %j - zero-padded day of year (e.g., 002, 145, 364...).
  • %m - zero-padded month (e.g., 01, 02...).
  • %M - zero-padded minutes (e.g., 01, 12, 59...).
  • %n - non-zero-padded month (e.g., 1, 2...).
  • %N - non-zero-padded minutes (e.g., 1, 12, 59).
  • %p - aM/PM (a.m. and p.m.).
  • %s - non-zero-padded seconds (e.g., 1, 2, 50...).
  • %S - zero-padded seconds (e.g., 01, 02, 50...).
  • %r - milliseconds (e.g., 1, 24, 500...).
  • %w - numeric week day where 0 is Sunday (e.g., 0, 1...).
  • %y - zero-padded year without the century part (e.g., 01, 13, 99...).
  • %Y - full year (e.g., 2001, 2013, 2099...).
  • %z - timezone in +HHMM or -HHMM format (e.g., +0200, -0530).
  • %x - locale-formatted date (platform dependent).
  • %X - locale-formatted time (platform dependent).
  • %% - literal percent character %.

Any characters that are not part of the %-something sequence are used as is in the final output.

The third and optional argument is the format configuration object. This object is used to customize the human-readable strings used in the output, such as month names and similar. Please refer to config.updateDefaultConfig() function's documentation for information on what this object may contain.

Example
let d = new Date(2009, 4, 1, 12, 33);

// US date format
strftime(d, '%n/%D/%Y'); // '5/1/2019'

// Using non-formatting characters
strftime(d, 'On %b %D at %i:%M %p'); // 'On May 1 at 12:33 p.m.'

// With 24-hour time
strftime(d, '%Y-%m-%d %H:%M'); // '2019-05-01 12:33'

// With localized short month names (this configuration is only used for
// this particular call, and the global defaults are not modified)
const srMnth = ['јан', 'феб', 'мар', 'апр', 'мај', 'јун', 'јул', 'авг',
  'сеп', 'окт', 'нов', 'дец'];
strftime(d, '%D. %b %Y.', {MNTH: srMnth}); // '1. мај 2019.'
Parameters:
Name Type Attributes Default Description
dt Date

The Date object to format

formatString string

The format of the output

config object <optional>
{}

Format configuration