ts-time-format/TimeFormatter

Consumption

import TimeFormatter, {TimeCompiler, TIME_COMPILERS, HOUR_COMPILER, HOUR12_COMPILER, MINUTE_COMPILER, SECOND_COMPILER, MS_COMPILER, HOUR12_NZ_COMPILER, HOUR_NZ_COMPILER, AM_PM_COMPILER} from "ts-time-format/TimeFormatter";

Default export#

Hierarchy#

Description#

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

Methods#

format (inherited from TemporalFormatter)#

(value: LocalTime, 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<LocalTime>[]): TimeFormatter

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

(pattern: string, compilers: Dictionary<TimeCompiler> = TIME_COMPILERS): TimeFormatter

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.

TimeCompiler#

Hierarchy#

Description#

TemporalCompiler for LocalTime.

Fields#

maxLength (inherited from TemporalCompiler)#

readonly maxLength: number

Maximum number of sequential characters supported in a pattern.

Methods#

compile (inherited from TemporalCompiler)#

(value: LocalTime, 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.

TIME_COMPILERS#

TIME_COMPILERS: Dictionary<TimeCompiler>

Default dictionary from pattern character to the respective compiler:

CharacterCompilerExamples per length
123
HHOUR_COMPILER505
KHOUR12_COMPILER505
mMINUTE_COMPILER505
sSECOND_COMPILER505
SMS_COMPILER448485
hHOUR12_NZ_COMPILER505
kHOUR_NZ_COMPILER505
aAM_PM_COMPILERAM

HOUR_COMPILER#

HOUR_COMPILER: TimeCompiler

Depending on length:

  1. Writes String(time.hour).
  2. Writes time.hour, prepending it with a leading zero if necessary to ensure 2 digits. For example, writes "06" for 6 o'clock.

Default pattern character is "H".

HOUR12_COMPILER#

HOUR12_COMPILER: TimeCompiler

Depending on length:

  1. Writes String(time.hour % 12). For example, writes "6" for both 6 AM and 6 PM.
  2. Writes time.hour % 12, prepending it with a leading zero if necessary to ensure 2 digits. For example, writes "06" for both 6 AM and 6 PM.

Default pattern character is "K".

MINUTE_COMPILER#

MINUTE_COMPILER: TimeCompiler

Depending on length:

  1. Writes String(time.minute).
  2. Writes time.minute, prepending it with a leading zero if necessary to ensure 2 digits. For example, writes "06" for 6 minutes.

Default pattern character is "m".

SECOND_COMPILER#

SECOND_COMPILER: TimeCompiler

Depending on length:

  1. Writes String(time.second).
  2. Writes time.second, prepending it with a leading zero if necessary to ensure 2 digits. For example, writes "06" for 6 seconds.

Default pattern character is "s".

MS_COMPILER#

MS_COMPILER: TimeCompiler

Writes the same number of the most significant fractions of a second as the length, truncating them down. For example, writes "4", "48" and "485" for 485 milliseconds and lengths 1, 2 and 3 respectively.

Default pattern character is "S".

HOUR12_NZ_COMPILER#

HOUR12_NZ_COMPILER: TimeCompiler

If time.hour % 12 === 0, writes "12". Else behaves like HOUR12_COMPILER.

Default pattern character is "h".

HOUR_NZ_COMPILER#

HOUR_NZ_COMPILER: TimeCompiler

If time.hour === 0, writes "24". Else behaves like HOUR_COMPILER.

Default pattern character is "k".

AM_PM_COMPILER#

AM_PM_COMPILER: TimeCompiler

Writes AM or PM. AM starts at 00:00:00.000 and ends at 11:59:59.999. PM starts at 12:00:00.000 and ends at 23:59:59.999.