DateTimeOffset.ToLocalTime() Method in C#


The DateTimeOffset.ToLocalTime() method in C# is used to convert the current DateTimeOffset object to a DateTimeOffset object that represents the local time.

Syntax

Following is the syntax −

public DateTimeOffset ToLocalTime ();

Example

Let us now see an example to implement the DateTimeOffset.ToLocalTime() method −

using System;
public class Demo {
   public static void Main() {
      DateTimeOffset local;
      DateTimeOffset dateTimeOffset = DateTimeOffset.Now;
      Console.WriteLine("DateTimeOffset = {0}", dateTimeOffset);
      local = dateTimeOffset.ToLocalTime();
      Console.WriteLine("Local Time = "+local.ToString());
   }
}

Output

This will produce the following output −

DateTimeOffset = 10/16/2019 11:20:35 AM +00:00
Local Time = 10/16/2019 11:20:35 AM +00:00

Example

Let us now see another example to implement the DateTimeOffset.ToLocalTime() method −

using System;
public class Demo {
   public static void Main() {
      DateTimeOffset local;
      DateTimeOffset dateTimeOffset = new DateTimeOffset(2019, 10, 12, 6, 10, 20, TimeSpan.Zero);
      Console.WriteLine("DateTimeOffset = {0}", dateTimeOffset);
      local = dateTimeOffset.ToLocalTime();
      Console.WriteLine("Local Time = "+local.ToString());
   }
}

Output

This will produce the following output −

DateTimeOffset = 10/12/2019 6:10:20 AM +00:00
Local Time = 10/12/2019 6:10:20 AM +00:00

Updated on: 12-Nov-2019

50 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements