DateTime.UtcNow提前4小时 [英] DateTime.UtcNow is 4 hours ahead

查看:106
本文介绍了DateTime.UtcNow提前4小时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到一些有关如何更正此问题的答案。我有一个类型为DateTime的对象。我分配了这个对象,如下所示。

I have seen some answers about how to correct this. I have an object of type DateTime. I have assigned this object as shown below.

obj.TimeStamp = DateTime.UtcNow;

我似乎找不到正确的组合或代码操作来获取正确的日期和时间。总是比当前时间早4小时。

I cannot seem to find the right combination or code manipulation to get the correct date and time. It is always exactly 4 hours ahead of what the current time is.

该软件将部署在各个时区。

This software will be deployed in various timezones.

有没有可靠的方法来确保时间戳记为在所有时区都是准确的。我的服务器时间设置正确。

Is there any reliable way to ensure the timestamp will be accurate across all time zones. My server time settings are correct.

谢谢。

推荐答案

我知道这个问题很古老,但是...

I know this question is old, but...

var localDate = DateTime.UtcNow.ToLocalTime()

https://msdn.microsoft.com/en-us/library/system.datetime.tolocaltime(v = vs.110).aspx

阅读MSDN文章中的注释。这不是万无一失的。但是,如果您正在寻找一种获取服务器时间的方法,这是一种快速简便的方法,可以尽可能地接近服务器,而不会变得太疯狂。

Read the notes in the MSDN article. This isnt't foolproof. But, if you are looking for a way to get the server time, this is a quick and easy way to get about as close as you can w/o getting too crazy.

这篇关于DateTime.UtcNow提前4小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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