DateTime.ToLocalTime() Method in C#

This method is used to convert the value of the current DateTime object to local time.

Syntax: public DateTime ToLocalTime ();

Return Value: This method returns an object whose Kind property is Local, and whose value is the local time equivalent to the value of the current DateTime object, or MaxValue if the converted value is too large to be represented by a DateTime object, or MinValue if the converted value is too small to be represented as a DateTime object.

Below programs illustrate the use of DateTime.ToLocalTime() Method

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// DateTime.ToLocalTime()
// Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // creating object of DateTime
        DateTime date = new DateTime(2011, 1,
                                1, 4, 0, 15);
  
        // Converts the value of the current 
        // DateTime object to local time.
        // using ToLocalTime() method;
        DateTime value = date.ToLocalTime();
  
        // Display the TimeSpan
        Console.WriteLine("local time is {0}", value);
    }
}

chevron_right


Output:

local time is 01/01/2011 04:00:15

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// DateTime.ToLocalTime()
// Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // creating object of DateTime
        DateTime date = DateTime.Now;
  
        // Converts the value of the current
        // DateTime object to local time.
        // using ToLocalTime() method;
        DateTime value = date.ToLocalTime();
  
        // Display the TimeSpan
        Console.WriteLine("local time is {0}", value);
    }
}

chevron_right


Output:

local time is 02/11/2019 05:13:37

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.