将datetime字符串转换为Utc [英] convert datetime string to Utc

查看:102
本文介绍了将datetime字符串转换为Utc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在格林尼治标准时间将日期时间字符串转换为utc时间格式.

how to convert datetime string to utc time format in GMT.

var x = "02/01/2017 10:00";
var z = DateTime.ParseExact(x, "ddd, dd MMM yyyy HH:mm:ss 'GMT'", CultureInfo.InvariantCulture);

推荐答案

实际上,您尝试过的事情是错误的,您可以做的是,获取等于给定日期的DateTime值,然后将其转换为UTC时间,尝试类似以下的内容:

Actually the thing you have tried is wrong, what you can do is, get the DateTime value equivalent to the given date and then convert them to the UTC time, try something like the following:

string inputDateStr = "02/01/2017 10:00";
DateTime inputDate;
if (DateTime.TryParseExact(inputDateStr, "MM/dd/yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out inputDate))
{
    Console.WriteLine("Date time Now  : {0} ", inputDate);
    Console.WriteLine("Date time UTC  : {0} ", inputDate.ToUniversalTime().ToString("ddd, dd MMM yyyy HH:mm:ss 'GMT'"));                
}

工作示例

这篇关于将datetime字符串转换为Utc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆