Noda Time , a better date and time API
What is Noda Time
Noda Time is a Nuget Package / Library developed by Jon Skeet with 96M downloads (until today), it is an alternative for dealing with date and time. If you are familiar with Java world then Noda Time is C# version of Joda Time
Why we should use it over default Date Time
-
Lack of clarity in
DateTime
especially when comparing local and UTC times -
DateTimeKind
property and various operations likeToUniversalTime()
andToLocalTime()
can lead to unexpected results -
DateTime
does not provide a comprehensive solution for handling time zones. -
TimeZoneInfo
, used in .NET, relies on Windows time zone IDs, which may differ from widely used Olson (tz) identifiers -
Unfortunately humanity did’ not agree on single calendar (Gregorian, Julian, Coptic and Buddhist) , NodaTime covers this space which default lacks.
-
Instead of maintaining offset in default Noda gives you facility of using
ZonedDateTime
-
I love their documentation , I have not seen such an amazing documentation yet. They really care and understand the needs of users
If I could not satisfy you then Mr. Jon Skeet will hopefully, Read this blog of Jon Skeet for in depth
How to use it in .NET
It is quite simple , you just need to install these Nuget packages
NodaTime
NodaTime.Testing
NodaTime.Serialization.JsonNet
These are few types available in .NET with NodaTime :
LocalDate
-including day,month,year , calendarDateTimeZone
- To shift in a specific time zone , pass it IanaZonedClock
- A clock associated with time zonePeriod
- To clculate time difference in calendrical termsDuration
- Fixed length of elapsed time (irrespective of calendar)Instant
- A instant on global timeline with nanosecond resolution
Instant now = SystemClock.Instance.GetCurrentInstant());
ZonedDateTime nowInIsoUtc = now. InUtc();
Duration duration = Duration.FromMinutes(3);
ZonedDateTime thenInIsoUtc = nowInIsoUtc + duration;
var london = DateTimeZoneProviders.Tzdb ["Europe/London"];
var localDate = new LocalDateTime(2012, 3, 27, 0, 45,00);
var before = london. AtStrictly(localDate);
It is difficult to include all of them in this post , better visit documentation of NodaTime
Jon was born on June 19th, 1976 (Gregorian). How old is he now, in the UK time zone?
LocalDate birthDate = new LocalDate (1976, 6, 19);
DateTimeZone zone = DateTimeZoneProviders.Tzdb["Europe/London"];
ZonedClock clock = SystemClock.Instance.InZone(zone);
LocalDate today clock.GetCurrentDate();
Console.WriteLine($"Today's date: {today: yyyy-MM-dd}");
Period age = Period.Between (birthDate, today);
Cosole.WriteLine($"Jon is: {age. Years} years old.");
The more you explore it more interesting things you will find.
Whenever you're ready, there are 3 ways I can help you:
- Subscribe to my youtube channel : For in-depth tutorials, coding tips, and industry insights.
- Promote yourself to 9,000+ subscribers : By sponsoring this newsletter
- Patreon community : Get access to all of my blogs and articles at one place