System.DateTimeOffset
Represents a point in time, including the offset from Coordinated Universal Time (UTC).
Description
The DateTimeOffset
structure represents a date and time value, along with an offset that distinguishes it from Coordinated Universal Time (UTC). This offset is the amount of time to add to UTC to get the local time.
The DateTimeOffset
structure is useful in scenarios where it is important to know both the date and time and the time zone's offset from UTC. This is especially true when dealing with data that may originate from different time zones or when performing date and time calculations that are sensitive to time zone differences.
Members
Constructors
Signature | Description |
---|---|
public DateTimeOffset(int year, int month, int day, int hour, int minute, int second, int millisecond, TimeSpan offset) | Initializes a new instance of the DateTimeOffset structure to the specified date and time values and offset from UTC. |
public DateTimeOffset(long ticks, TimeSpan offset) | Initializes a new instance of the DateTimeOffset structure to the specified number of ticks and offset from UTC. |
public DateTimeOffset(DateTime dateTime) | Initializes a new instance of the DateTimeOffset structure using the provided DateTime value. |
public DateTimeOffset(DateTime dateTime, TimeSpan offset) | Initializes a new instance of the DateTimeOffset structure using the provided DateTime value and offset from UTC. |
Properties
Signature | Description |
---|---|
public DateTime DateTime { get; } | Gets the date and time of the DateTimeOffset value, expressed as a local time. |
public DateTime DateTimeUtc { get; } | Gets the date and time of the DateTimeOffset value, expressed as UTC. |
public int Day { get; } | Gets the day of the month, expressed as a value from 1 to 31. |
public int Hour { get; } | Gets the hour of the day, expressed as a value from 0 to 23. |
public int Millisecond { get; } | Gets the milliseconds of the DateTimeOffset value. |
public int Minute { get; } | Gets the minutes of the DateTimeOffset value. |
public int Month { get; } | Gets the month of the year, expressed as a value from 1 to 12. |
public int Second { get; } | Gets the seconds of the DateTimeOffset value. |
public long Ticks { get; } | Gets the value of this instance expressed as the number of 100-nanosecond intervals that have elapsed since January 1, 0001, at 12:00:00 A.M. (midnight) coordinated universal time (UTC). |
public TimeSpan TimeOfDay { get; } | Gets the time of day for the current DateTimeOffset value. |
public int Year { get; } | Gets the year of the DateTimeOffset value. |
Methods
Signature | Description |
---|---|
public static DateTimeOffset AddDays(DateTimeOffset dateTimeOffset, double days) | Returns a new DateTimeOffset value that adds the specified number of days to the value of this instance. |
public static DateTimeOffset Now | Gets the current date and time on this computer, expressed as the coordinated universal time (UTC) date and time. |
public static DateTimeOffset UtcNow | Gets the current date and time on this computer, expressed as the coordinated universal time (UTC) date and time. |
public string ToString() | Converts the value of the current DateTimeOffset object to its equivalent string representation. |
public string ToString(string? format) | Converts the value of the current DateTimeOffset object to its equivalent string representation, using the specified format. |
public bool Equals(DateTimeOffset other) | Determines whether two DateTimeOffset instances represent the same point in time. |
Example Usage
using System; public class Example { public static void Main() { // Create a DateTimeOffset for a specific date and time with an offset DateTimeOffset localTime = new DateTimeOffset( 2023, 10, 27, 10, 30, 0, 0, TimeSpan.FromHours(-5) // UTC-5 ); Console.WriteLine($"Local Time: {localTime}"); Console.WriteLine($"UTC Time: {localTime.ToUniversalTime()}"); Console.WriteLine($"Date: {localTime.Date.ToShortDateString()}"); Console.WriteLine($"Time: {localTime.TimeOfDay}"); // Add days to the DateTimeOffset DateTimeOffset futureTime = localTime.AddDays(7); Console.WriteLine($"Seven days later: {futureTime}"); // Current UTC time DateTimeOffset nowUtc = DateTimeOffset.UtcNow; Console.WriteLine($"Current UTC: {nowUtc}"); } }