Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
DateTimeOffset.ToOffset() Method in C#
The DateTimeOffset.ToOffset() method in C# is used to convert a DateTimeOffset object to a different time zone offset while preserving the same absolute point in time. This method adjusts the local time component to match the new offset, ensuring the UTC time remains unchanged.
Syntax
Following is the syntax −
public DateTimeOffset ToOffset(TimeSpan offset)
Parameters
offset: A TimeSpan representing the time zone offset to convert to. The offset must be between -14 and +14 hours.
Return Value
Returns a new DateTimeOffset object with the specified offset, representing the same moment in time but displayed in the target time zone.
Using ToOffset() to Convert Time Zones
Example
using System;
public class Demo {
public static void Main() {
DateTimeOffset dateTimeOffset = new DateTimeOffset(2019, 9, 10, 4, 20, 30, new TimeSpan(-5, 0, 0));
Console.WriteLine("Original DateTimeOffset = {0}", dateTimeOffset);
DateTimeOffset res = dateTimeOffset.ToOffset(new TimeSpan(-7, 0, 0));
Console.WriteLine("Converted to -07:00 = {0}", res);
Console.WriteLine("UTC time remains: {0}", dateTimeOffset.UtcDateTime);
}
}
The output of the above code is −
Original DateTimeOffset = 9/10/2019 4:20:30 AM -05:00 Converted to -07:00 = 9/10/2019 2:20:30 AM -07:00 UTC time remains: 9/10/2019 9:20:30 AM
Converting Between Different Offsets
Example
using System;
public class Demo {
public static void Main() {
DateTimeOffset easternTime = new DateTimeOffset(2019, 9, 10, 4, 20, 30, new TimeSpan(-5, 0, 0));
Console.WriteLine("Eastern Time: {0}", easternTime);
DateTimeOffset pacificTime = easternTime.ToOffset(new TimeSpan(-8, 0, 0));
Console.WriteLine("Pacific Time: {0}", pacificTime);
DateTimeOffset utcTime = easternTime.ToOffset(TimeSpan.Zero);
Console.WriteLine("UTC Time: {0}", utcTime);
}
}
The output of the above code is −
Eastern Time: 9/10/2019 4:20:30 AM -05:00 Pacific Time: 9/10/2019 1:20:30 AM -08:00 UTC Time: 9/10/2019 9:20:30 AM +00:00
Converting with Custom Offsets
Example
using System;
public class Demo {
public static void Main() {
DateTimeOffset original = new DateTimeOffset(2019, 9, 10, 4, 20, 30, new TimeSpan(5, 0, 0));
Console.WriteLine("Original: {0}", original);
DateTimeOffset customOffset = original.ToOffset(new TimeSpan(3, 30, 0));
Console.WriteLine("Custom +3:30 offset: {0}", customOffset);
DateTimeOffset negativeOffset = original.ToOffset(new TimeSpan(-9, 30, 0));
Console.WriteLine("Custom -9:30 offset: {0}", negativeOffset);
}
}
The output of the above code is −
Original: 9/10/2019 4:20:30 AM +05:00 Custom +3:30 offset: 9/10/2019 2:50:30 AM +03:30 Custom -9:30 offset: 9/9/2019 1:50:30 PM -09:30
Conclusion
The DateTimeOffset.ToOffset() method provides an efficient way to convert time zone representations while maintaining the same absolute moment in time. This method is particularly useful for displaying the same timestamp across different time zones or when working with international applications that need to show localized times.
