Dates, times, and time zones
In addition to the basic DateTime structure, .NET provides the following classes that support working with time zones:
Use this class to work with any time zone that is predefined on a system, to create new time zones, and to easily convert dates and times from one time zone to another. For new development, use the TimeZoneInfo class instead of the TimeZone class.
Use this structure to work with dates and times whose offset (or difference) from UTC is known. The DateTimeOffset structure combines a date and time value with that time's offset from UTC. Because of its relationship to UTC, an individual date and time value unambiguously identifies a single point in time. This makes a DateTimeOffset value more portable from one computer to another than a DateTime value.
Starting with .NET 6, the following types are available:
Use this structure when working with a value that only represents a date. The date represents the entire day, from the start of the day to the end.
DateOnlyhas a range of
9999-12-31. And, this type represents the month, day, and year combination without a specific time. If you previously used a
DateTimetype in your code to represent a date that disregarded the time, use this type in its place.
Use this structure to represent a time without a date. The time represents the hours, minutes, and seconds of a non-specific day.
TimeOnlyhas a range of
23:59:59.9999999. This type can be used to replace
TimeSpantypes in your code when you used those types to represent a time.
The next section provides the information that you need to work with time zones and to create time zone-aware applications that can convert dates and times from one time zone to another.
In this section
Time zone overview
Discusses the terminology, concepts, and issues involved in creating time zone-aware applications.
Choosing between DateTime, DateTimeOffset, TimeSpan, and TimeZoneInfo
Discusses when to use the DateTime, DateTimeOffset, and TimeZoneInfo types when working with date and time data.
Finding the time zones defined on a local system
Describes how to enumerate the time zones found on a local system.
How to: Enumerate time zones present on a computer
Provides examples that enumerate the time zones defined in a computer's registry and that let users select a predefined time zone from a list.
How to: Access the predefined UTC and local time zone objects
Describes how to access Coordinated Universal Time and the local time zone.
How to: Create time zones without adjustment rules
Describes how to create a custom time zone that does not support the transition to and from daylight saving time.
How to: Create time zones with adjustment rules
Describes how to create a custom time zone that supports one or more transitions to and from daylight saving time.
How to: Save time zones to an embedded resource
Describes how to create a custom time zone and save its information in a resource file.
How to: Restore time zones from an embedded resource
Describes how to instantiate custom time zones that have been saved to an embedded resource file.
Performing arithmetic operations with dates and times
Discusses the issues involved in adding, subtracting, and comparing DateTime and DateTimeOffset values.
How to: Use time zones in date and time arithmetic
Discusses how to perform date and time arithmetic that reflects a time zone's adjustment rules.
Converting times between time zones
Describes how to convert times from one time zone to another.
How to: Resolve ambiguous times
Describes how to resolve an ambiguous time by mapping it to the time zone's standard time.
How to: Let users resolve ambiguous times
Describes how to let a user determine the mapping between an ambiguous local time and Coordinated Universal Time.