📜  C#中的DateTime.ToLocalTime()方法

📅  最后修改于: 2021-05-29 19:03:30             🧑  作者: Mango

此方法用于将当前DateTime对象的值转换为本地时间。

下面的程序说明了DateTime.ToLocalTime()方法的用法

范例1:

// 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);
    }
}
输出:
local time is 01/01/2011 04:00:15

范例2:

// 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);
    }
}
输出:
local time is 02/11/2019 05:13:37

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.datetime.tolocaltime?view=netframework-4.7.2