ISO 8601 :: C# library that converts Duration Patterns from the ISO 8601 and resolves limitations with the convertion with System.Xml.
You can find more information about the ISO 8601 and the lexical reprentation in the next link. ISO 8601 - Lexical Representation - Duration
The format of a lexical representation is:
PnYnMnDTnHnMnS
(Compatible for .NET Framework v4.6.1 or greater, and .NET Core 2.0 or greater)
Iso8601Duration Converter - v1.0.0
struct DurationStruct
- Struct to work with dates and times easily.
public int Years;
public int Months;
public int Days;
public int Hours;
public int Minutes;
public int Seconds;
string NormalizeDuration(string pattern)
- Converts a general pattern in a correct and a well formed pattern.
// "PT61S" to "PT1M1S"
var periodBuilder = new PeriodBuilder();
var pattern = "PT61S";
var wellFormedPattern = periodBuilder.NormalizeDuration(inputPattern);
DurationStruct ToDurationStruct(TimeSpan timeSpan)
- Converts from TimeSpan to DurationStruct.
var periodBuilder = new PeriodBuilder();
var durationStruct = periodBuilder.ToDurationStruct(new TimeSpan(0, 0, 120));
DurationStruct ToDurationStruct(string pattern)
- Converts from TimeSpan to DurationStruct.
var periodBuilder = new PeriodBuilder();
var durationStruct = periodBuilder.ToDurationStruct("P1Y");
string ToString(TimeSpan timeSpan)
- Converts from TimeSpan to Duration Pattern.
var periodBuilder = new PeriodBuilder();
var durationStruct = periodBuilder.ToString(new TimeSpan(0, 120, 0));
string ToString(DurationStruct durationStruct)
- Converts from DurationStruct to Duration Pattern.
var periodBuilder = new PeriodBuilder();
var durationStruct = periodBuilder.ToString("PT2H");
TimeSpan ToTimeSpan(string pattern)
- Converts from Duration Pattern to TimeSpan.
var periodBuilder = new PeriodBuilder();
var durationStruct = periodBuilder.ToTimeSpan("PT2H");
TimeSpan ToTimeSpan(DurationStruct durationStruct)
- Converts from DurationStruct to TimeSpan.
var periodBuilder = new PeriodBuilder();
var durationStruct = periodBuilder.ToTimeSpan(new DurationStruct() { Minutes = 1440 });