https://github.com/thomaslevesque/iso8601durationhelper
Small library to handle ISO8601 durations in C#
https://github.com/thomaslevesque/iso8601durationhelper
Last synced: about 1 year ago
JSON representation
Small library to handle ISO8601 durations in C#
- Host: GitHub
- URL: https://github.com/thomaslevesque/iso8601durationhelper
- Owner: thomaslevesque
- License: apache-2.0
- Created: 2018-10-16T15:20:10.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-02-07T15:10:41.000Z (over 3 years ago)
- Last Synced: 2025-03-27T10:37:55.788Z (over 1 year ago)
- Language: C#
- Size: 43 KB
- Stars: 33
- Watchers: 4
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Iso8601DurationHelper
[](https://www.nuget.org/packages/Iso8601DurationHelper)
[](https://ci.appveyor.com/project/thomaslevesque/iso8601duration)
[](https://ci.appveyor.com/project/thomaslevesque/iso8601duration/build/tests)
A small library to handle ISO8601 durations (e.g. `P1Y` for 1 year, `PT2H30M` for 2 hours and 30 minutes) in C#.
Some libraries attempt to parse these durations to `TimeSpan`, but it doesn't really make sense, because `TimeSpan` doesn't have a concept of month, so they just translate `P1M` to 30 days. This is wrong because all months don't have the same number of days; January 1 + 1 month should be February 1, not January 31.
This library introduces a `Duration` struct with operators to add a duration to or subtract a duration from a date, with the proper semantics. For instance:
```csharp
var startDate = new DateTime(2018, 1, 1);
var duration = Duration.Parse("P2M");
var endDate = startDate + duration; // 2018/03/01
```