DateTimeOffset.ToLocalTime() Method in C#

The DateTimeOffset.ToLocalTime() method in C# converts a DateTimeOffset object to the local time zone of the system where the code is running. This method adjusts both the time value and the offset to match the local time zone.

Syntax

Following is the syntax for the ToLocalTime() method −

public DateTimeOffset ToLocalTime();

Return Value

This method returns a new DateTimeOffset object that represents the same moment in time but adjusted to the local time zone. The offset will reflect the local system's time zone offset from UTC.

Using ToLocalTime() with Current Time

The following example demonstrates converting the current UTC time to local time −

using System;

public class Demo {
   public static void Main() {
      DateTimeOffset utcTime = DateTimeOffset.UtcNow;
      DateTimeOffset localTime = utcTime.ToLocalTime();
      
      Console.WriteLine("UTC Time = " + utcTime.ToString());
      Console.WriteLine("Local Time = " + localTime.ToString());
      Console.WriteLine("Time Zone Offset = " + localTime.Offset.ToString());
   }
}

The output of the above code is −

UTC Time = 10/16/2019 4:20:35 PM +00:00
Local Time = 10/16/2019 12:20:35 PM -04:00
Time Zone Offset = -04:00:00

Using ToLocalTime() with Custom DateTimeOffset

This example shows how to convert a specific UTC time to local time −

using System;

public class Demo {
   public static void Main() {
      DateTimeOffset utcOffset = new DateTimeOffset(2019, 10, 12, 14, 30, 0, TimeSpan.Zero);
      DateTimeOffset localOffset = utcOffset.ToLocalTime();
      
      Console.WriteLine("Original UTC = " + utcOffset.ToString());
      Console.WriteLine("Converted Local = " + localOffset.ToString());
      
      TimeSpan difference = localOffset.Offset - utcOffset.Offset;
      Console.WriteLine("Time difference = " + difference.TotalHours + " hours");
   }
}

The output of the above code is −

Original UTC = 10/12/2019 2:30:00 PM +00:00
Converted Local = 10/12/2019 10:30:00 AM -04:00
Time difference = -4 hours

Converting Between Different Time Zones

Here's an example showing conversion from a specific time zone to local time −

using System;

public class Demo {
   public static void Main() {
      DateTimeOffset tokyoTime = new DateTimeOffset(2019, 10, 15, 23, 45, 0, TimeSpan.FromHours(9));
      DateTimeOffset localTime = tokyoTime.ToLocalTime();
      
      Console.WriteLine("Tokyo Time (UTC+9) = " + tokyoTime.ToString());
      Console.WriteLine("Local Time = " + localTime.ToString());
      Console.WriteLine("Local DateTime = " + localTime.DateTime.ToString());
      Console.WriteLine("Local Offset = " + localTime.Offset.ToString());
   }
}

The output of the above code is −

Tokyo Time (UTC+9) = 10/15/2019 11:45:00 PM +09:00
Local Time = 10/15/2019 10:45:00 AM -04:00
Local DateTime = 10/15/2019 10:45:00 AM
Local Offset = -04:00:00

How It Works

The ToLocalTime() method performs two key operations: it converts the time value to the equivalent moment in the local time zone and updates the offset to match the local system's UTC offset. The actual moment in time remains the same − only the representation changes to reflect the local time zone.

ToLocalTime() Conversion Process Original 14:30 UTC+0 (2 PM UTC) Local Time 10:30 UTC-4 (10:30 AM EST) ToLocalTime()

Conclusion

The DateTimeOffset.ToLocalTime() method converts any DateTimeOffset to the local system time zone, adjusting both the time value and offset accordingly. This method is essential when displaying time values to users in their local time zone, ensuring consistent time representation across different geographical locations.

Updated on: 2026-03-17T07:04:35+05:30

181 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements