ts-time-format/DateFormatter

Consumption

import DateFormatter, {DateCompiler, DATE_COMPILERS, ERA_COMPILER, YEAR_COMPILER, YEAR_OF_ERA_COMPILER, DAY_OF_YEAR_COMPILER, MONTH_COMPILER, DAY_OF_MONTH_COMPILER, QUARTER_OF_YEAR_COMPILER, WEEK_BASED_YEAR_COMPILER, WEEK_OF_WEEK_BASED_YEAR_COMPILER, DAY_OF_WEEK_TEXT_COMPILER, DAY_OF_WEEK_COMPILER} from "ts-time-format/DateFormatter";

Default export#

Hierarchy#

Description#

Formatter of LocalDate to a string. You may initialize a formatter explicitly by using of method, or implicitly by using ofPattern method. See DATE_COMPILERS for supported pattern characters. See parsePattern for general pattern writing guidelines.

Methods#

format (inherited from TemporalFormatter)#

(value: LocalDate, context?: any): string

value
Value to format.
context
Context object (see TemporalCompiler).
returns
Formatted string.
Formats value to a string as concatenation of write method call results on all components.

Static methods#

of#

(components: TemporalFormatComponent<LocalDate>[]): DateFormatter

components
Ordered array of format components.
returns
Formatter.
Constructs a formatter explicitly by components.
ofPattern#

(pattern: string, compilers: Dictionary<DateCompiler> = DATE_COMPILERS): DateFormatter

pattern
Pattern string.
compilers
Dictionary from supported pattern characters to the respective compilers.
returns
Formatter.
Constructs a formatter implicitly by pattern. See parsePattern for details.

DateCompiler#

Hierarchy#

Description#

TemporalCompiler for LocalDate.

Fields#

maxLength (inherited from TemporalCompiler)#

readonly maxLength: number

Maximum number of sequential characters supported in a pattern.

Methods#

compile (inherited from TemporalCompiler)#

(value: LocalDate, length: number, context: any = DEFAULT_TEMPORAL_CONTEXT): string

value
Value to compile.
length
Number of sequential characters used in a pattern.
context
Context object.
returns
Compiled string.
Compiles value to a string based on number of sequential characters specified in length and compiler-dependent context object which can be used, in particular, to specify language-specific textual strings.

DATE_COMPILERS#

DATE_COMPILERS: Dictionary<DateCompiler>

Default dictionary from pattern character to the respective compiler:

CharacterCompilerExamples per length
12345
GERA_COMPILERADAnno DominiA
uYEAR_COMPILER20090920092009
yYEAR_OF_ERA_COMPILER20090920092009
DDAY_OF_YEAR_COMPILER505005
M or LMONTH_COMPILER202FebFebruaryF
dDAY_OF_MONTH_COMPILER505
Q or qQUARTER_OF_YEAR_COMPILER101Q11st quarter
YWEEK_BASED_YEAR_COMPILER20090920092009
wWEEK_OF_WEEK_BASED_YEAR_COMPILER505
EDAY_OF_WEEK_TEXT_COMPILERTueTuesdayT
e or cDAY_OF_WEEK_COMPILER202TueTuesdayT

ERA_COMPILER#

ERA_COMPILER: DateCompiler

Depending on length:

  1. Writes context.eraShortNames[date.era.value] (default to "BC", "AD")
  2. Writes context.eraNames[date.era.value] (default to "Before Christ", "Anno Domini")
  3. Writes context.eraAbbreviations[date.era.value] (default to "B", "A")

Default pattern character is "G".

YEAR_COMPILER#

YEAR_COMPILER: DateCompiler

Depending on length:

  1. Writes String(date.year).
  2. Writes two lower-rank digits of date.year. For example, writes "09" for 2009 year. Includes sign for BC, e.g. "-09" for -9 year.
  3. Same as 1.
  4. Same as 1.

Default pattern character is "u".

YEAR_OF_ERA_COMPILER#

YEAR_OF_ERA_COMPILER: DateCompiler

Same as YEAR_COMPILER, but based on date.yearOfEra. See Era for year convertion rules.

Default pattern character is "y".

DAY_OF_YEAR_COMPILER#

DAY_OF_YEAR_COMPILER: DateCompiler

Depending on length:

  1. Writes String(date.dayOfYear).
  2. or 3. Writes date.dayOfYear, prepending it with leading zeros if necessary to ensure 2/3 digits. For example, writes "023" for 23rd of January and length 3.

Default pattern character is "D".

MONTH_COMPILER#

MONTH_COMPILER: DateCompiler

Depending on length:

  1. Writes String(date.month.value).
  2. Writes date.month.value, prepending it with a leading zero if necessary to ensure 2 digits. For example, writes "02" for February.
  3. Writes context.monthShortNames[date.month.value - 1] (default to "Jan", "Feb"...)
  4. Writes context.monthNames[date.month.value - 1] (default to "January", "February"...)
  5. Writes context.monthAbbreviations[date.month.value - 1] (default to "J", "F"...)

Default pattern character is "M" or "L".

DAY_OF_MONTH_COMPILER#

DAY_OF_MONTH_COMPILER: DateCompiler

Depending on length:

  1. Writes String(date.dayOfMonth).
  2. Writes date.dayOfMonth, prepending it with a leading zero if necessary to ensure 2 digits. For example, writes "06" for 6th of February.

Default pattern character is "d".

QUARTER_OF_YEAR_COMPILER#

QUARTER_OF_YEAR_COMPILER: DateCompiler

Depending on length:

  1. Writes String(date.quarterOfYear).
  2. Writes date.quarterOfYear, prepending it with a leading zero to ensure 2 digits. For example, writes "04" for 4th quarter.
  3. Writes context.quarterShortNames[date.quarterOfYear - 1] (default to "Q1", "Q2"...)
  4. Writes context.quarterNames[date.quarterOfYear - 1] (default to "1st quarter", "2nd quarter"...)

Default pattern character is "Q" or "q".

WEEK_BASED_YEAR_COMPILER#

WEEK_BASED_YEAR_COMPILER: DateCompiler

Same as YEAR_COMPILER, but based on date.weekBasedYear.

Default pattern character is "Y".

WEEK_OF_WEEK_BASED_YEAR_COMPILER#

WEEK_OF_WEEK_BASED_YEAR_COMPILER: DateCompiler

Depending on length:

  1. Writes String(date.weekOfWeekBasedYear).
  2. Writes date.weekOfWeekBasedYear, prepending it with a leading zero if necessary to ensure 2 digits. For example, writes "09" for 9th week of year.

Default pattern character is "w".

DAY_OF_WEEK_TEXT_COMPILER#

DAY_OF_WEEK_TEXT_COMPILER: DateCompiler

Depending on length:

  1. Writes context.dayOfWeekShortNames[date.dayOfWeek.value - 1] (default to "Mon", "Tue"...)
  2. Writes context.dayOfWeekNames[date.dayOfWeek.value - 1] (default to "Monday", "Tuesday"...)
  3. Writes context.dayOfWeekAbbreviations[date.dayOfWeek.value - 1] (default to "M", "T")

Default pattern character is "E".

DAY_OF_WEEK_COMPILER#

DAY_OF_WEEK_COMPILER: DateCompiler

Depending on length:

  1. Writes String(date.dayOfWeek.value).
  2. Writes date.dayOfWeek.value, prepending it with a leading zero to ensure 2 digits. For example, writes "04" for Thursday.
  3. or 4. or 5. Same as DAY_OF_WEEK_TEXT_COMPILER's 1, 2 and 3 respectively.

Default pattern character is "e" or "c".