C#中的DateTime.ToLocalTime()方法用于将当前DateTime对象的值转换为本地时间。
以下是语法-
public DateTime ToLocalTime ();
现在让我们看一个实现DateTime.ToLocalTime()方法的示例-
using System; public class Demo { public static void Main() { DateTime d = DateTime.Now; Console.WriteLine("Date = {0}", d); DateTime res = d.ToLocalTime(); Console.WriteLine("Local Time = {0}", res); } }
输出结果
这将产生以下输出-
Date = 10/16/2019 8:28:29 AM Local Time = 10/16/2019 8:28:29 AM
现在让我们来看另一个实现DateTime.ToLocalTime()方法的示例-
using System; public class Demo { public static void Main() { DateTime d = new DateTime(2019, 10, 11, 9, 10, 45); Console.WriteLine("Date = {0}", d); DateTime res = d.ToLocalTime(); Console.WriteLine("Local Time = {0}", res); } }
输出结果
这将产生以下输出-
Date = 10/11/2019 9:10:45 AM Local Time = 10/11/2019 9:10:45 AM