将UTC中的DateTime转换为我的“本地”时间? [英] Converting DateTime in UTC to my "local" time?

查看:192
本文介绍了将UTC中的DateTime转换为我的“本地”时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与MongoDB合作,尝试使用正确的<$ c $将 BsonDate 反序列化为 DateTime c>种类。当 DateTime 序列化到MongoDB时,它以UTC格式保存,反序列化时,它也以UTC格式返回。

I am working with a MongoDB trying to deserialize a BsonDate to a DateTime with the proper Kind. When a DateTime is serialized to MongoDB it is saved in UTC format, when it is deserialized it is returned as a UTC format too.

我的问题是,我似乎无法将我从蒙哥退回的 DateTime 转换为本地的 DateTime (UTC到EST)。我很确定我可以通过删除时区的UTC时间偏移( AddHours )来解决问题,但是我很确定 DateTime 可以为我做到这一点,并且可以处理应用程序运行所在的任何时区。

My issue is that I cannot seem to be able to convert the DateTime I am given back from Mongo to my local DateTime (UTC to EST). I am pretty sure I can get around my issue by just removing the UTC time offset (AddHours) for my timezone but I am pretty sure that DateTime can do this for me and would handle any time zone the application is ran in.

// Deserializing the DateTime object
DateTime eventTimeStamp = (DateTime)aDoc[MongoStrings.Log_Field_TimeStamp];
Console.Out.WriteLine("UtcDate: " + eventTimeStamp);
Console.Out.WriteLine("Locale : " + eventTimeStamp.Kind);

// First attempt at conversion
DateTime localTime = DateTime.SpecifyKind(eventTimeStamp, DateTimeKind.Local);
Console.Out.WriteLine("NewTime: " + localTime);
Console.Out.WriteLine("Locale : " + localTime.Kind);

// Another attempt at conversion
DateTime localTime2 = new DateTime(eventTimeStamp.Ticks, DateTimeKind.Local);
Console.Out.WriteLine("NewTim2: " + localTime2);
Console.Out.WriteLine("Locale : " + localTime2.Kind);

上面的代码产生以下输出

The code above produces the following output

UtcDate: 1/29/2016 2:54:05 PM
Locale : Utc
NewTime: 1/29/2016 2:54:05 PM
Locale : Local
NewTim2: 1/29/2016 2:54:05 PM
Locale : Local

我生成该日志的本地时间是 9:54:05 AM

My local time when that log was produced is 9:54:05 AM.

推荐答案

这可能对您有用:

// This is your code
// Deserializing the DateTime object
DateTime eventTimeStamp = (DateTime)aDoc[MongoStrings.Log_Field_TimeStamp];
Console.Out.WriteLine("UtcDate: " + eventTimeStamp);
Console.Out.WriteLine("Locale : " + eventTimeStamp.Kind);

// This is new code
Console.Out.WriteLine("LocalDate: " + eventTimeStamp.ToLocalTime());

原因是您的localTime变量设置为与UTC中的时间戳完全相同的时间戳,您只是说应该将其视为当地时间。但是这种设置为本地时间并不能转换时间,它只是说这是什么时间,以便其他方法(例如ToLocalTime)知道该怎么做...

The reasoning would be that your localTime variables are set to the exactly same time stamp as the one in UTC, you just say that it should be taken as a local time. But this setting as local time does no conversion of times, it just says what kind of time this is so that other methods (like ToLocalTime) know what to do ...

如果要在变量中使用该本地时间,则可能是这样的:

If you want that local time in a variable, then it might be like this:

DateTime localTime = eventTimeStamp.ToLocalTime();

我想它无需设置种类就可以工作。如果没有,您知道如何设置种类...

And I guess it will work without setting the kind. If not, you know how to set the kind ...

这篇关于将UTC中的DateTime转换为我的“本地”时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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