Skip to content

Format syntax

Arseniy Svechkarev edited this page Sep 16, 2023 · 14 revisions

This section explains how to use format syntax

1. Basics of formatting

Format is a string that may contain special symbols, such as:

  • H - hours
  • M - minutes
  • S - seconds
  • L - either milliseconds or hundredths of a second or tenths of a second (detailed explanation later)

For example, let's consider a format like MM:SS. It contains minutes and seconds. If the current time is 1 minute and 37 seconds, then the result of formatting will be 01:37. If the current time is 1 hour, 2 minutes and 9 seconds, the result will be 122:09 and so on.

If you need to use special format characters (H, M, S, or L) as a plain text, you can put escape symbol # before them.

For example, if the format is HH#H MM#M and time is 2 hours 47 minutes, the result will be 02H 47M.

Some examples of formatting:
Format Time (milliseconds) Output
MM:SS 11478 00:11
MM:SS 146229 02:26
MM:SS 8246387 137:26
MMm SSs 11478 00m 11s
MMm SSs 146229 02m 26s
MMm SSs 8246387 137m 26s
HH:MM 394724 00:06
HH:MM 8262249 02:17
HH:MM 71476396 19:51
HH#H MM#M and SS#S 394724 00H 06M and 34S
HH#H MM#M and SS#S 8262249 02H 17M and 42S
HH#H MM#M and SS#S 71476396 19H 51M and 16S

2. "L" character

Character L can be formatted as milliseconds, hundredths of a second or tenths of a second.

If there are no other special symbols, then L is formatted as milliseconds. If there are other special symbols, then L is formatted depending on how many L characters are there in the format. If there is one character — it is formatted as tenths of a second, two - hundredths of a second, three and more — milliseconds.

Consider format "M:SS.LL" and time 36698 milliseconds (36 seconds and 698 milliseconds). In this case, since the amount of L characters in the format is 2, last digit of 698 milliseconds will be omitted, and the result will be "0:36.69".

Examples of formatting with L character:

Format Time (milliseconds) Output
SS:L 367 00:3
SS:L 1322 01:3
SS:L 15991 15:9
SS:LL 367 00:36
SS:LL 1322 01:32
SS:LL 15991 15:99
SS:LLL 367 00:367
SS:LLL 1322 01:322
SS:LLL 15991 15:991
SS:LLLL 367 00:0367
SS:LLLL 1322 01:0322
SS:LLLL 15991 15:0991
LLLL 367 0367
LLLL 1322 1322
LLLL 15991 15991

3. Invalid formats

Some formats are invalid. There are three types of such formats:

  • Formats that don't contain any special characters ("H", "M", "S" or "L").

  • Formats that contain same special symbols separately, like "HH:HH" or "MM:SS ML". Those formats are invalid there is no unambiguous way to replace special symbols with corresponding time. Keep in mind that same special symbols can be placed together, but not separately ("MMM:SSS" - is correct format, "MM:SS:MM" - not)

  • Formats that contain special symbols in are in unacceptable combination:

    • Input format has hours with seconds or milliseconds, but does not have minutes.
    • Input format has hours, minutes, and milliseconds, but does not have seconds.
    • Input format has minutes and milliseconds, but does not have seconds.

    Such formats are considered wrong, because there is no definite way to format remaining time.