将当前 DateTime 对象的值转换为 C# 中的 UTC
要将当前 DateTime 对象的值转换为协调世界时 (UTC),代码如下所示 −
示例
using System; public class Demo { public static void Main() { DateTime d = new DateTime(2019, 12, 11, 7, 11, 25); Console.WriteLine("Date = {0}", d); DateTime res = d.ToUniversalTime(); Console.WriteLine("String representation = {0}", res); } }
输出
这将产生以下输出 −
Date = 11/11/2019 7:11:25 AM String representation = 11/11/2019 7:11:25 AM
示例
现在让我们看另一个示例 −
using System; public class Demo { public static void Main() { DateTime localDate, universalDate; String str = "11/11/2019 4:10:55"; localDate = DateTime.Parse(str); universalDate = localDate.ToUniversalTime(); Console.WriteLine("Local time = {0} ", localDate); Console.WriteLine("Universal time = {0} ", universalDate); } }
输出
这将产生以下输出 −
Local time = 11/11/2019 4:10:55 AM Universal time = 11/11/2019 4:10:55 AM
广告