Patterns for Offset values

The Offset type supports the following patterns:

Standard Patterns

The following standard patterns are supported:

  • f: Full format, displaying all information including fractional seconds. Typical pattern text: +HH:mm:ss.fff
  • l: Long format, displaying information down to the second. Typical pattern text: +HH:mm:ss
  • m: Medium format, displaying information down to the minute. Typical pattern text: +HH:mm
  • s: Short format, displaying information down to the hour. Typical pattern text: +HH
  • g: General pattern. Formatting depends on the value passed in:
    • If the offset has fractional seconds, the full format is used; otherwise
    • If the offset has seconds, the long format is used; otherwise
    • If the offset has minutes, the medium format is used; otherwise
    • The short format is used When parsing, the other standard format patterns are tried one at a time. This is the default format pattern.
  • G: As g, but using Z for an offset of 0, as if it were Z-prefixed. (See below.)

Custom Patterns

The following custom pattern characters are supported for offsets. See custom pattern notes for general notes on custom patterns, including characters used for escaping and text literals.

Character Meaning Example
Z This can only occur at the start of a pattern, as a prefix to a normal pattern. When it's used, an offset of zero is always formatted as "Z", and "Z" will be parsed as a zero offset. When formatting, a non-zero offset falls back to the remainder of the pattern. When parsing, a non-Z value is always parsed by the remainder of the pattern, and a result of a zero offset is still acceptable. (So a pattern of "ZHH:mm" can still parse a value of "00:00" even though it would be formatted as "Z".) Zero: ZHH:mm => Z
5 hours: ZHH:mm => 05:00
H or HH Number of hours in the offset. HH is zero-padded; H is not. H:mm => 7:30
HH:mm => 07:30
m or mm Number of minutes within the hour. mm is zero-padded; m is not. 5 minutes: m:ss => 5:00
5 minutes: mm:ss => 05:00
s or ss Number of seconds within the minute. ss is zero-padded; s is not. 5 seconds: s.fff => 5.000
5 seconds: ss.fff => 05.000
f, ff or fff The fractional second part of the offset, using exactly the specified number of characters. Trailing digits are truncated towards zero. 1 second, 340 milliseconds: s.fff => 340
1 second, 340 milliseconds: s.ff => 34
1 second, 340 milliseconds: s.f => 3
F, FF or FFF The fractional second part of the offset, using at most the specified number of characters. Trailing digits are truncated towards zero, and trailing insignificant zeroes are truncated. If this comes after a decimal separator and the value is zero, the decimal separator is also truncated. 1 second, 340 milliseconds: s.FFF => 1.34
1 second, 340 milliseconds: s.FF => 1.34
1 second, 340 milliseconds: s.F => 1.3
Exactly 1 second: s.F => 1
+ The sign of the value, always specified whether positive or negative. The character used will depend on the format provider; + and - are used by the invariant culture. A positive offset is used when local time is ahead of UTC (e.g. Europe) and a negative offset is used when local time is behind UTC (e.g. America). Positive value: +HH:mm => +07:30
Negative value: +HH:mm => -07:30
- The sign of the value, only specified when the value is negative. The character used will depend on the format provider; - is used by the invariant culture. Positive value: -HH:mm => 07:30
Negative value: -HH:mm => -07:30
. This is always a period ("."); not a culture-sensitive decimal separator as one might expect. This follows the example of other standard libraries, however odd it may appear. The only difference between a period and any other literal character is that when followed by a series of "F" characters, the period will be removed if there are no fractional seconds. 12 seconds, 500 milliseconds (en-US): ss.FFF => 12.5
12 seconds, 500 milliseconds (fr-FR): ss.FFF => 12.5
: The time separator for the format provider; colon in the invariant culture. HH:mm => 07:30