It has been a while since i wrote last post but i am back with one more Nuget package which is NodaTime.
Many of you have always worked with Date time or time zone in your current project or in past and if you try to recall you might have faced some problems because while doing time and timezone related programming you would know how much headache it can be.
NodaTime tries to make that process a little bit better and it is written by the famous Jon Skeet.
Let us say more in details like Noda Time is an alternative date and time API for .NET. It helps you to think about your data more clearly, and express operations on that data more precisely.
To install NodaTime , run the following command in the Package Manager Console :-
Now the question comes why do we need NodaTime? or What’s wrong with DateTime anyway?
Well The standard DateTime data type in .NET is not setup properly to deal with time zones. The DateTime type does not allow you to define a date and time in a specific time zone.
And Jon Skeet has written more on this which is here :- what-wrong-with-datetime-anyway
Let us see some of the examples :-
//Instant represents time from epoch
Instant now = SystemClock.Instance.Now;
// Convert an instant to a ZonedDateTime
ZonedDateTime nowInIsoUtc = now.InUtc();
// Create a duration
Duration duration = Duration.FromMinutes(3);
// Add it to our ZonedDateTime
ZonedDateTime thenInIsoUtc = nowInIsoUtc + duration;
// Time zone support (multiple providers)
var london = DateTimeZoneProviders.Tzdb["Europe/London"];
// Time zone conversions
var localDate = new LocalDateTime(2012, 3, 27, 0, 45, 00);
var before = london.AtStrictly(localDate);
Well there is whole website made for NodaTime and there are lots of scenarios mentioned which we cant include all here so better visit this for more examples:-
From a unit testing perspective NodaTime makes the process also a bit easier as it exposes an IClock interface to get the current time, which can be mocked or you can even use their own FakeClock class to assist with testing.
For that you need to run the following command in the Package Manager Console:-
NodaTime is also available in Git hub which you can find here.
So, finally i will suggest to use NodaTime not just for expressiveness and correctness of your code but also because it is just Awesome!
Happy Coding!