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
10
votes
1 answer

How do you deal with 'Ambiguous Time' in .net?

I need to convert a 'DateTime' value from different timezones to UTC and vice-versa. I use TimeZoneInfo to do this. But, the issue is when the 'Day Light Saving' time change happens. For example, this year, next time the time change happens at…
usp
  • 797
  • 3
  • 10
  • 24
10
votes
3 answers

With Noda Time, how to create a LocalDateTime using a LocalDate and LocalTime

I have a LocalDate and a LocalTime and would like to simply create a LocalDateTime struct from them. I thought of the following extension method which I believe would be the fastest but for an obscure reason the field localTime.TickOfMillisecond…
Erwin Mayer
  • 18,076
  • 9
  • 88
  • 126
9
votes
1 answer

NSwag: How do you Use Custom Value Object Types in C# -> Swagger -> C# client?

I have an API that uses Noda Time types in both input and output. The types are serialized to strings in the JSON using the default Noda Time serialization format (which basically is the ISO-8601 format). I have an object looking something like…
Mikkel R. Lund
  • 2,336
  • 1
  • 31
  • 44
9
votes
1 answer

What is the neatest way to find the last possible instant of a LocalDate, in a particular time-zone?

The pre-existing SQL Server database we work with stores a 'period' as inclusive start -> inclusive end UTC DateTime values. (Both start & end columns are datetime2(7), automatically-converted to System.DateTime instances of DateTimeKind.UTC before…
Ben Jenkinson
  • 1,806
  • 1
  • 16
  • 31
9
votes
1 answer

How to parse multiple formats with Noda Time?

I like the Noda Time handling of formatting and parsing values using the various *Pattern types in the NodaTime.Text namespace. However, user input is often not as regular as a single format. For example, our app uses the time format "h:mm tt" but…
Jeff Walker Code Ranger
  • 4,634
  • 1
  • 43
  • 62
8
votes
2 answers

NodaTime Invalid DateTime.Kind for Instant.FromDateTimeUtc

I'm trying to get ahold of this timezone issue we are having. We would like to store all DateTimes in UTC, and then convert the DateTime to the user's timezone. We decided to use NodaTime for this, as it seems like the right approach. However, we…
Jannik
  • 401
  • 1
  • 5
  • 17
8
votes
1 answer

Nodatime create a ZonedDateTime given a time and timezone

Can anyone give me the most straightforward way to create a ZonedDateTime, given "4:30pm" and "America/Chicago". I want this object to represent that time for the current date in that timezone. Thanks! I tried this... but it seems to actually give…
Brian Rice
  • 3,107
  • 1
  • 35
  • 53
8
votes
1 answer

How to handle when timezone goes backwards in the future

I am generating two sets of repeating events in seperate loop iterations but am having a conflict when comparing the generated results for conflicts. This seems to be when the times go backwards and I am unsure how to solve this? The first repeat…
Dizzle
  • 1,026
  • 13
  • 26
8
votes
1 answer

Deserialize a date query parameter of the form yyyy-MM-dd into a noda time LocalDate object using ASP.NET Web API

I'm investigating the use of NodaTime LocalDate to replace our existing use of of the BCL DateTime/DateTimeOffset classes. We have run into a number of timezone related issues with our code due to our misunderstanding of the arguably ambiguous…
Ryan Taylor
  • 8,740
  • 15
  • 65
  • 98
8
votes
1 answer

noda time iana mapping of Etc/UTC to Windows timezone

I have a global website that is passing the IANA timezone id to the server and using Noda Time to map to the Windows Timezone in c# 5 web app. "Etc/UTC" is being passed to the server but Noda Time can not map it to a Windows time zone. How can I…
user3772576
  • 93
  • 1
  • 5
8
votes
1 answer

Using NodaTime, how to convert an Instant to the corresponding system's ZonedDateTime?

I have an event log in a database with the events' datetime stored as UTC time. In my application, where I'm using NodaTime, I get the value as a DateTime and then convert it to an Instant: var instant =…
MarioVW
  • 2,225
  • 3
  • 22
  • 28
8
votes
1 answer

What is the System.TimeZoneInfo.IsDaylightSavingTime equivalent in NodaTime?

System.TimeZoneInfo has a method called IsDaylightSavingTime, which takes a DateTime object and returns true if the specified datetime falls in the DST for that timezone. Is there an equivalent function in NodaTime or another way to achieve the same…
ash
  • 236
  • 1
  • 7
7
votes
1 answer

How to tell entity framework how to save instances of a custom type (that can be stored as a scalar)

One of my entity classes would be possible to store in a sql server database as a BIGINT. My question is: How do I get a Entity Framework context to know how to store and retrieve instances of my entity class? More detail. I'm using Noda Time,…
Levin Magruder
  • 1,905
  • 18
  • 26
7
votes
3 answers

Why can't I add months to OffsetDateTime?

I'm using NodaTime to manage dates and time zones in a .Net Core WebApi. One of the type in the library is OffsetDateTime, which is very similar to DateTimeOffset from the .Net framework. I use it everywhere to manipulate dates in an explicit and…
Morgoth
  • 237
  • 2
  • 11
7
votes
1 answer

Deserializing LocalTime from JSON using JSON.NET and NodaTime results in NodaTime.Text.UnparsableValueException

I am having an issue deserializing time values into LocalTime- granted, I am pretty new to NodaTime. I want to import a web service result which lists a time in "HH:mm" format. I get an exception unless I use a time in "hh:mm:ss.fff" format. Is…
David Rogers
  • 4,010
  • 3
  • 29
  • 28
1 2
3
30 31