在C#中将当前DateTime对象的值转换为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