Class LocalDateTimePattern
Represents a pattern for parsing and formatting LocalDateTime values.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Implements
Inherited Members
Namespace: NodaTime.Text
Assembly: NodaTime.dll
Syntax
public sealed class LocalDateTimePattern : IPattern<LocalDateTime>
Properties
BclRoundtrip
Gets an invariant local date/time pattern which is ISO-8601 compatible, providing up to 7 decimal places
of sub-second accuracy which are always present (including trailing zeroes). This is compatible with the
BCL round-trip formatting of DateTime values with a kind of "unspecified".
This corresponds to the text pattern "uuuu'-'MM'-'dd'T'HH':'mm':'ss'.'fffffff". It does not necessarily
round-trip all
LocalDateTime
values as it will lose sub-tick information. Use
FullRoundtripWithoutCalendar for full precision.
Since 1.4.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static LocalDateTimePattern BclRoundtrip { get; }
Property Value
Type | Description |
---|---|
LocalDateTimePattern | An invariant local date/time pattern which is ISO-8601 compatible, providing up to 7 decimal places of sub-second accuracy which are always present (including trailing zeroes). |
Remarks
This pattern corresponds to the 'o' and 'O' standard patterns.
DateHourIso
Gets an invariant local date/time pattern which is ISO-8601 compatible, with a precision of just hours.
This corresponds to the text pattern "uuuu'-'MM'-'dd'T'HH".
Since 3.2.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static LocalDateTimePattern DateHourIso { get; }
Property Value
Type | Description |
---|---|
LocalDateTimePattern | An invariant local time pattern which is ISO-8601 compatible, with no sub-hour precision. |
DateHourMinuteIso
Gets an invariant local date/time pattern which is ISO-8601 compatible, with precision of just minutes.
This corresponds to the text pattern "uuuu'-'MM'-'dd'T'HH':'mm".
Since 3.2.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static LocalDateTimePattern DateHourMinuteIso { get; }
Property Value
Type | Description |
---|---|
LocalDateTimePattern | An invariant local time pattern which is ISO-8601 compatible, with no sub-minute precision. |
ExtendedIso
Gets an invariant local date/time pattern which is ISO-8601 compatible, providing up to 9 decimal places
of sub-second accuracy. (These digits are omitted when unnecessary.)
This corresponds to the text pattern "uuuu'-'MM'-'dd'T'HH':'mm':'ss;FFFFFFFFF".
Since 1.4.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static LocalDateTimePattern ExtendedIso { get; }
Property Value
Type | Description |
---|---|
LocalDateTimePattern | An invariant local date/time pattern which is ISO-8601 compatible, providing up to 9 decimal places of sub-second accuracy. |
Remarks
This pattern corresponds to the 'S' standard pattern ("longer sortable").
FullRoundtrip
Gets an invariant local date/time pattern which round trips values including the calendar system.
This corresponds to the text pattern "uuuu'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffff '('c')'".
Since 1.4.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static LocalDateTimePattern FullRoundtrip { get; }
Property Value
Type | Description |
---|---|
LocalDateTimePattern | An invariant local date/time pattern which round trips values including the calendar system. |
Remarks
This pattern corresponds to the 'R' standard pattern.
FullRoundtripWithoutCalendar
Gets an invariant local date/time pattern which round trips values, but doesn't include the calendar system.
It provides up to 9 decimal places of sub-second accuracy which are always present (including trailing zeroes).
This corresponds to the text pattern "uuuu'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffff". It will
round-trip all LocalDateTime values if the calendar system of the template value is the same
as the calendar system of the original value.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static LocalDateTimePattern FullRoundtripWithoutCalendar { get; }
Property Value
Type | Description |
---|---|
LocalDateTimePattern | An invariant local date/time pattern which is ISO-8601 compatible, providing up to 7 decimal places of sub-second accuracy which are always present (including trailing zeroes). |
Remarks
This pattern corresponds to the 'r' standard pattern.
GeneralIso
Gets an invariant local date/time pattern which is ISO-8601 compatible, down to the second.
This corresponds to the text pattern "uuuu'-'MM'-'dd'T'HH':'mm':'ss".
Since 1.4.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static LocalDateTimePattern GeneralIso { get; }
Property Value
Type | Description |
---|---|
LocalDateTimePattern | An invariant local date/time pattern which is ISO-8601 compatible, down to the second. |
Remarks
This pattern corresponds to the 's' standard pattern ("shorter sortable").
PatternText
Gets the pattern text for this pattern, as supplied on creation.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public string PatternText { get; }
Property Value
Type | Description |
---|---|
string | The pattern text for this pattern, as supplied on creation. |
TemplateValue
Get the value used as a template for parsing: any field values unspecified
in the pattern are taken from the template.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public LocalDateTime TemplateValue { get; }
Property Value
Type | Description |
---|---|
LocalDateTime | The value used as a template for parsing. |
TwoDigitYearMax
Maximum two-digit-year in the template to treat as the current century.
If the value parsed is higher than this, the result is adjusted to the previous century.
This value defaults to 30. To create a pattern with a different value, use WithTwoDigitYearMax(int).
Since 3.2.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public int TwoDigitYearMax { get; }
Property Value
Type | Description |
---|---|
int | The value used for the maximum two-digit-year, in the range 0-99 inclusive. |
VariablePrecisionIso
Gets an invariant local date/time pattern which can parse any ISO-8601 compatible value with a calendar date
(in extended format, that is, with separators), regardless of precision in the time part.
Valid values for time include "just hours", "hours and minutes", "hours, minutes and seconds",
and values with fractions of seconds (as far as nanoseconds). The time part must be present, however; this pattern
will not parse date-only values. (It will also not parse ordinal dates or week dates, as described in ISO-8601.)
Since 3.2.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static IPattern<LocalDateTime> VariablePrecisionIso { get; }
Property Value
Type | Description |
---|---|
IPattern<LocalDateTime> | An invariant local date/time pattern which is ISO-8601 compatible for all time precisions. |
Remarks
This is expressed as an IPattern<T> rather than a LocalDateTimePattern,
as it has no single pattern text.
Methods
AppendFormat(LocalDateTime, StringBuilder)
Formats the given value as text according to the rules of this pattern,
appending to the given StringBuilder.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public StringBuilder AppendFormat(LocalDateTime value, StringBuilder builder)
Parameters
Type | Name | Description |
---|---|---|
LocalDateTime | value | The value to format. |
StringBuilder | builder | The StringBuilder to append to. |
Returns
Type | Description |
---|---|
StringBuilder | The builder passed in as builder . |
Create(string, CultureInfo)
Creates a pattern for the given pattern text and culture, with a template value of midnight on 2000-01-01.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static LocalDateTimePattern Create(string patternText, CultureInfo cultureInfo)
Parameters
Type | Name | Description |
---|---|---|
string | patternText | Pattern text to create the pattern for |
CultureInfo | cultureInfo | The culture to use in the pattern |
Returns
Type | Description |
---|---|
LocalDateTimePattern | A pattern for parsing and formatting local date/times. |
Remarks
See the user guide for the available pattern text options.
Exceptions
Type | Condition |
---|---|
InvalidPatternException | The pattern text was invalid. |
Create(string, CultureInfo, LocalDateTime)
Creates a pattern for the given pattern text, culture, and template value.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static LocalDateTimePattern Create(string patternText, CultureInfo cultureInfo, LocalDateTime templateValue)
Parameters
Type | Name | Description |
---|---|---|
string | patternText | Pattern text to create the pattern for |
CultureInfo | cultureInfo | The culture to use in the pattern |
LocalDateTime | templateValue | Template value to use for unspecified fields |
Returns
Type | Description |
---|---|
LocalDateTimePattern | A pattern for parsing and formatting local date/times. |
Remarks
See the user guide for the available pattern text options.
Exceptions
Type | Condition |
---|---|
InvalidPatternException | The pattern text was invalid. |
CreateWithCurrentCulture(string)
Creates a pattern for the given pattern text in the current thread's current culture.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static LocalDateTimePattern CreateWithCurrentCulture(string patternText)
Parameters
Type | Name | Description |
---|---|---|
string | patternText | Pattern text to create the pattern for |
Returns
Type | Description |
---|---|
LocalDateTimePattern | A pattern for parsing and formatting local date/times. |
Remarks
See the user guide for the available pattern text options. Note that the current culture
is captured at the time this method is called - it is not captured at the point of parsing
or formatting values.
Exceptions
Type | Condition |
---|---|
InvalidPatternException | The pattern text was invalid. |
CreateWithInvariantCulture(string)
Creates a pattern for the given pattern text in the invariant culture.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static LocalDateTimePattern CreateWithInvariantCulture(string patternText)
Parameters
Type | Name | Description |
---|---|---|
string | patternText | Pattern text to create the pattern for |
Returns
Type | Description |
---|---|
LocalDateTimePattern | A pattern for parsing and formatting local date/times. |
Remarks
See the user guide for the available pattern text options.
Exceptions
Type | Condition |
---|---|
InvalidPatternException | The pattern text was invalid. |
Format(LocalDateTime)
Formats the given local date/time as text according to the rules of this pattern.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public string Format(LocalDateTime value)
Parameters
Type | Name | Description |
---|---|---|
LocalDateTime | value | The local date/time to format. |
Returns
Type | Description |
---|---|
string | The local date/time formatted according to this pattern. |
Parse(string)
Parses the given text value according to the rules of this pattern.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public ParseResult<LocalDateTime> Parse(string text)
Parameters
Type | Name | Description |
---|---|---|
string | text | The text value to parse. |
Returns
Type | Description |
---|---|
ParseResult<LocalDateTime> | The result of parsing, which may be successful or unsuccessful. |
Remarks
This method never throws an exception (barring a bug in Noda Time itself). Even errors such as
the argument being null are wrapped in a parse result.
WithCalendar(CalendarSystem)
Creates a pattern like this one, but with the template value modified to use
the specified calendar system.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public LocalDateTimePattern WithCalendar(CalendarSystem calendar)
Parameters
Type | Name | Description |
---|---|---|
CalendarSystem | calendar | The calendar system to convert the template value into. |
Returns
Type | Description |
---|---|
LocalDateTimePattern | A new pattern with a template value in the specified calendar system. |
Remarks
Care should be taken in two (relatively rare) scenarios. Although the default template value is supported by all Noda Time calendar systems, if a pattern is created with a different template value and then this method is called with a calendar system which doesn't support that date, an exception will be thrown. Additionally, if the pattern only specifies some date fields, it's possible that the new template value will not be suitable for all values.
WithCulture(CultureInfo)
Creates a pattern for the same original pattern text as this pattern, but with the specified
culture.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public LocalDateTimePattern WithCulture(CultureInfo cultureInfo)
Parameters
Type | Name | Description |
---|---|---|
CultureInfo | cultureInfo | The culture to use in the new pattern. |
Returns
Type | Description |
---|---|
LocalDateTimePattern | A new pattern with the given culture. |
WithTemplateValue(LocalDateTime)
Creates a pattern like this one, but with the specified template value.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public LocalDateTimePattern WithTemplateValue(LocalDateTime newTemplateValue)
Parameters
Type | Name | Description |
---|---|---|
LocalDateTime | newTemplateValue | The template value for the new pattern, used to fill in unspecified fields. |
Returns
Type | Description |
---|---|
LocalDateTimePattern | A new pattern with the given template value. |
WithTwoDigitYearMax(int)
Creates a pattern like this one, but with a different TwoDigitYearMax value.
Since 3.2.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public LocalDateTimePattern WithTwoDigitYearMax(int twoDigitYearMax)
Parameters
Type | Name | Description |
---|---|---|
int | twoDigitYearMax | The value to use for TwoDigitYearMax in the new pattern, in the range 0-99 inclusive. |
Returns
Type | Description |
---|---|
LocalDateTimePattern | A new pattern with the specified maximum two-digit-year. |