Questions tagged [nodatime]

Noda Time is a .NET library designed to simplify the correct handling of dates and times in the .NET environment.

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.

It borrows many concepts from Joda Time, the industry standard date and time handling library for Java. It is an idiomatic port - not a direct translation. It has been specifically tailored for use in the .Net Framework.

There are many reasons to use Noda Time instead of the normal BCL date and time types, including:

  • Support for IANA/Olson time zones such as America/New_York.

  • Distinct representations of different concepts: Instead of using DateTime to represent everything, there are different types for dates, times, values with time zones, values with just UTC offsets, etc.

  • Reduced surprise: All Noda Time types work consistently in a predictable manner. By contrast, the DateTime type has different behaviors depending on its Kind property. For examples of the type of surprises that are avoided, read "What's Wrong with DateTime Anyway?"

  • Testability: You can easily mock the system clock using the IClock interface, and you have to go out of your way if you want to be affected by the computer's local time zone. By contrast, DateTime.Now is difficult to mock, and is bound to the system time zone by default.

Noda Time also has the ability to work with Windows time zones, and can convert between them. It also contains zone.tab data, which can be used to correlate time zones with a country code, or locate time zones on a map.

Resources:

The primary author of Noda Time is Jon Skeet, of Stack Overflow fame.

See also:

452 questions
5
votes
3 answers

Long form name of timezone in NodaTime

In NodaTime, how do you find the long form name of a timezone given the tz timezone id? For example, if I supply "America/Los_Angeles", I should get "Pacific Standard Time" back.
govin
  • 6,445
  • 5
  • 43
  • 56
5
votes
2 answers

How to ignore reference to jetbrains.annotations.dll in NodaTime nuget package xml documentation file

I lean quite heavily on Resharper context actions to generate boilerplate code. The Check parameter for null context action had, up until recently, generated code that performs the null check and throws accordingly. However, now it is adding a…
Matt Caton
  • 3,473
  • 23
  • 25
5
votes
1 answer

Proper serialization of LocalTime through WebAPI

I'm having trouble serialising and de-serialising NodaTime's LocalTime through a WebAPI. Class definition public class ExampleClass { public LocalTime ExampleLocalTime { get; set; } } Try to serialize the output // create example object var…
wonea
  • 4,783
  • 17
  • 86
  • 139
5
votes
1 answer

Is there a way to represent partial dates in Noda Time?

I'm attempting to represent a company's founded date in C#. I occasionally have a full day, month, and year, but more often I only have a month and year, or sometimes a year alone. Is there a way I can represent this in NodaTime, in such a way as to…
David Pfeffer
  • 38,869
  • 30
  • 127
  • 202
5
votes
1 answer

DateTime to LocalDate (NodaTime)

I have a DateTime that needs to be converted to a NodaTime LocalDate. Can this be done? I have this: DateTime modifiedDate = File.GetLastWriteTime(file); And I want this: LocalTime modifiedDate = File.GetLastWriteTime(file); But it looks like …
user2475310
  • 733
  • 4
  • 13
  • 19
5
votes
2 answers

Nodatime calculation of years/months/days in X days

Say I have 678 days, how to calculate how many years, months and days are there from that moment? Duration duration = Duration.FromStandardDays(678); Instant now = SystemClock.Instance.Now; Instant future = now + duration; // I have to convert…
5
votes
1 answer

What is the ServiceStack.Text-equivalent of Json.NET Converters, for example when applied to NodaTime types?

How can I control the serialization/deserialization of custom types (such as NodaTime.LocalDateTime) with ServiceStack.Text? Json.NET provides Converters for this, so that each time a class contains a custom type, the corresponding custom…
Erwin Mayer
  • 18,076
  • 9
  • 88
  • 126
5
votes
1 answer

How do you use NodaTime Classes in a PropertyGrid?

I'm converting some application code to use NodaTime classes instead of System.DateTime. Part of my application uses the PropertyGrid control to allow a user to edit a class containing both a LocalDate and an Instant. Without changing anything,…
scott
  • 1,127
  • 1
  • 12
  • 21
5
votes
1 answer

Noda Time Instance value not deserializing correctly

I'm currently using RavenDB to store a object that uses Noda Time's Instant class to store dates. RavenDb stores the value as "ArrivalTime": { "Ticks": 13507658019037497 }, but when query the object it is always null and starts at the…
lai tang
  • 169
  • 1
  • 6
5
votes
1 answer

How do I create an NHibernate IUserType for a Time in Sql Server 2008/2012?

I am trying to create an NHibernate IUserType for the Noda Time LocalTime type which would logically map to a time type in Sql Server 2008/2012. I am able to get values saving and loading from the database. However, I can't write queries involving…
Jeff Walker Code Ranger
  • 4,634
  • 1
  • 43
  • 62
4
votes
3 answers

How to get list of timezones in NodaTime

How do I get a list of "time zones" from NodaTime such that I can make a UI like the below for my users to choose from? I want to show the UTC offset and then the appropriate cities/countries/locations. It doesn't need to be exactly like the below,…
Andrew Bullock
  • 36,616
  • 34
  • 155
  • 231
4
votes
1 answer

Errors Creating App Package (UWP, Appx) - fatal error CMF1106: failed to open input PDB file for reading

I'm getting errors when Creating App Packages for a UWP Win 10 application. On my machine the error is: C:\Users\Developer\.nuget\packages\nodatime\2.4.0\lib\netstandard2.0\NodaTime.pdb : fatal error CMF1106: failed to open input PDB file for…
tymtam
  • 31,798
  • 8
  • 86
  • 126
4
votes
1 answer

How to format NodaTime date string globally in ASP.NET Core 2.1?

Currently, I am trying to use the JsonFormatters for serializing a string in ISO 8601 spec. format in my startup config, but could not get it to work. My Startup Config is as follows: services.AddMvcCore( (options) => { options.SslPort =…
JSON
  • 1,583
  • 4
  • 31
  • 63
4
votes
2 answers

How to get a DateTimeOffset having local time information

I have these input strings: var timeStr = "03:22"; var dateStr = "2018/01/12"; var format = "yyyy/MM/dd"; var timeZone = "Asia/Tehran"; This is the information of a time that I have, the the timeStr is in Asia/Tehran time zone and is not in…
mehrandvd
  • 8,806
  • 12
  • 64
  • 111
4
votes
2 answers

Replace TimeZone

We are trying to build basic event calendaring functionality that allows a user to create an event and specify a start time at a given month, day, year, hour, and minute as well as a time zone (System.TimeZoneInfo.Id). The CMS system generates the…
Alexander Staroselsky
  • 37,209
  • 15
  • 79
  • 91