The best equivalent I've found is the combination of a DateTimeOffset + TimeZoneInfo. Is that the best approach, create a structure that contains both of these classes and insure they stay consistent?
-
2[NodaTime](https://nodatime.org/) should look familiar to you. It started as a port of Joda Time, which was the basis for the overhaul of Java's temporal types that include ZonedDateTime. – madreflection Jul 08 '20 at 22:02
-
Is that you need? https://nodatime.org/3.0.x/api/NodaTime.ZonedDateTime.html – Jul 08 '20 at 22:03
-
Does this answer your question? [Creating a DateTime in a specific Time Zone in c#](https://stackoverflow.com/questions/246498/creating-a-datetime-in-a-specific-time-zone-in-c-sharp) – Jul 08 '20 at 22:07
-
@OlivierRogier Unfortunately no. I need for example to be able to set to a datetime in MountaintTime, and the offset will change depending on if we're in standard or daylight time. NodaTime may be the answer although it feels like overkill for our need. – David Thielen Jul 08 '20 at 22:30
1 Answers
No, there is not a built-in type similar to ZonedDateTime
in the .NET base class library.
As others pointed out, if you're looking for a temporal model similar to Java's Joda Time or java.time
, then consider using Noda Time. It has a ZonedDateTime
structure.
If you don't want to use Noda Time, and you really need a single object containing date time offset and time zone, then your suggested approach of a struct with DateTimeOffset
and TimeZoneInfo
fields makes sense. Here are some additional tips:
It's important you design this as an immutable struct, in otherwords - take input only from the constructor. Expose only property getters over the fields. Don't expose the fields directly, and don't provide setters on the properties.
Be aware of how you want to handle situations where the offset of a
DateTimeOffset
is not the correct offset for the given time zone. You may want to adjust it, or you may want to throw an exception.You may need to provide custom serialization for your struct, and you may need to deconstruct it if saving to a database. In either scenario, keep only the string
Id
of theTimeZoneInfo
component. Don't try to serialize or store the entire object.
That said - you might want to reconsider if you need such an object. In many cases, simply using the DateTimeOffset
and TimeZoneInfo
separately are sufficient.

- 230,703
- 74
- 448
- 575