简体   繁体   English

将datetime字符串转换为Utc

[英]convert datetime string to Utc

how to convert datetime string to utc time format in GMT. 如何在格林尼治标准时间将datetime字符串转换为utc时间格式。

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

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: 实际上,您尝试过的事情是错误的,您可以做的是,获取等于给定日期的DateTime值,然后将其转换为UTC时间,请尝试以下操作:

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'"));                
}

Working Example 工作实例

I am not sure what you wanted to do. 我不确定您想做什么。 But, Universal Time , Zulu time , and UTC are effectively modern names for Greenwich Mean Time (GMT) . 但是, 通用时间祖鲁时间UTC实际上是格林威治标准时间(GMT)的现代名称。 So both the times will be same. 因此,这两个时间将是相同的。 You can use the DateTime.ToUniversalTime() or DateTime.UtcNow method to get the UTC time. 您可以使用DateTime.ToUniversalTime()DateTime.UtcNow方法获取UTC时间。

It's not completely clear what you are asking... UTC isn't a format, it's a timezone, equivalent to GMT (well, actually that isn't strictly true, but for the purposes of this question it should be ok. 目前还不清楚您要问的是什么... UTC不是一种格式,它是一个时区,相当于格林尼治标准时间(嗯,实际上这并不是严格意义上的,但是出于这个问题的目的,应该没问题。

What point in time is your string representing? 您的字符串代表什么时间? Is it UTC? 是UTC吗? Or is it the local time in Moscow? 还是莫斯科当地时间? You need to have the answer to that, because they are completely different times. 您需要得到答案,因为它们是完全不同的时间。

If the string represents a UTC time, then you can do something like this: 如果字符串表示UTC时间,则可以执行以下操作:

// first parse it to a DateTime object. //首先将其解析为DateTime对象。 Notice that the format string corresponds with the string you have - in your code, they were completely different. 请注意,格式字符串与您拥有的字符串相对应-在您的代码中,它们完全不同。

var dateTimeWithUnspecifiedKind = DateTime.ParseExact(x, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture);

So how you have a datetime object. 那么你如何拥有一个datetime对象。 It has a "Kind" property of Unspecified. 它具有“未指定”的“种类”属性。 So it isn't storing any information to specify that it's supposed to be UTC, rather than Moscow or New York time. 因此,它不会存储任何信息来指定它应该是UTC,而不是莫斯科或纽约时间。

// so we do this... //这样我们就可以...

var dateTimeAsUtc = DateTime.SpecifyKind(dateTimeWithUnspecifiedKind, DateTimeKind.Utc);

That means, "keep the numbers the same, but make a note that the datetime is to be interpreted as a UTC datetime". 这意味着“保持数字相同,但请注意,日期时间将被解释为UTC日期时间”。

Then, if you want to convert it back into a string, you can do something like: 然后,如果要将其转换回字符串,则可以执行以下操作:

var s = dateTimeAsUtc.ToString("O");

Which will give you a nice representation: 这会给你一个很好的表示:

2017-01-02T10:00:00.0000000Z

You are lucky you are interested in UTC. 您很幸运,您对UTC感兴趣。 The datetime class can only do UTC, "local" (whatever that is), or "Unspecified". datetime类只能执行UTC,“本地”(无论是什么)或“未指定”。 It's not super useful for this sort of thing. 对于这种事情不是超级有用。 DateTimeOffset is a bit better - it's basically a DateTime but it also stores the difference between UTC and the offset that's in force at the time. DateTimeOffset更好一点-它基本上是一个DateTime,但它还存储UTC和当时有效的偏移量之间的差。

That may be all you need. 那可能就是您所需要的。 If you ever need real clarity around this stuff though, take a look at Noda time - an alternative set of date and time classes for .NET. 但是,如果您真的需要弄清楚这些内容,请看一下Noda时间-.NET的另一组日期和时间类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM