[英]How does C# DateTime determine it's Now and Today times
Couldn't find anything on the internet nor the documentation on this.在互联网上找不到任何东西,也找不到关于此的文档。 Where does C# get it's
DateTime.Now
or DateTime.Today
from? C# 从哪里得到它的
DateTime.Now
或DateTime.Today
? These are localized values.这些是本地化的值。 Is it determined by Windows?
是由Windows决定的吗? Or does IIS influence this?
或者 IIS 会影响这个吗? I want to know what exactly parses the real now time to C# and what can influence it.
我想知道究竟是什么将实时时间解析为 C# 以及什么会影响它。
I am having some weird API behavior where some things are returned hours before they should.我有一些奇怪的 API 行为,其中有些东西会提前几个小时返回。 Which is most likely related to time zones.
这很可能与时区有关。 VPS and client are in the same time zone so I am wondering what can influence such behavior.
VPS 和客户端在同一个时区,所以我想知道什么会影响这种行为。
I am doing a hard ...FromDate.Date <= DateTime.Today.Date
and for some reason I sometimes get records that are "from" tomorrow.我正在努力
...FromDate.Date <= DateTime.Today.Date
并且出于某种原因,我有时会得到明天“来自”的记录。 Where 2021-02-21 <= 2021-02-20
seems to be "true". 2021-02-21 <= 2021-02-20
似乎是“真实的”。 True meaning that the DateTime.Today
seems to be off.真正的意思是
DateTime.Today
似乎已经关闭。 Only seeing this behavior around after 10 PM so hours before actual midnight suggesting it has something to do with time zones.仅在晚上 10 点之后看到这种行为,在实际午夜之前几个小时,这表明它与时区有关。
If you have any idea on this behavior please tell me如果您对此行为有任何想法,请告诉我
Thanks to @Matthew Watson for helping me get here.感谢@Matthew Watson 帮助我到达这里。
The DateTime.Now
is received from the system.从系统接收
DateTime.Now
。 First it will get UtcNow
and then applying a local offset (if there is any).首先它将获得
UtcNow
,然后应用本地偏移量(如果有的话)。
See source code line 1051见源代码第 1051 行
The DateTime.Today
property just returns DateTime.Now.Date
DateTime.Today
属性只返回DateTime.Now.Date
See source code line 1262见源代码行 1262
UtcNow is received by the internal extern method GetSystemTimeWithLeapSecondsHandling
. UtcNow 由内部外部方法
GetSystemTimeWithLeapSecondsHandling
接收。 Which I couldn't find a implementation for.我找不到实现。
See source code查看源代码
From my experience, DateTime.Now
or DateTime.Today
checks your server's time.根据我的经验,
DateTime.Now
或DateTime.Today
检查您的服务器的时间。 If you are running your application from the server, most likely it is checking your server time如果您从服务器运行您的应用程序,很可能它正在检查您的服务器时间
So technically, according to Microsoft documentation, the DateTime.Now property returns the DateTime object that is stored on the machine to the current date and time, interpreted as local time.所以从技术上讲,根据微软文档,DateTime.Now 属性将存储在机器上的 DateTime object 返回到当前日期和时间,解释为本地时间。 On the other side, DateTime.Today, is an object set to today's date, with the time component set to 00:00:00.
另一方面,DateTime.Today 是一个 object 设置为今天的日期,时间组件设置为 00:00:00。 The Today property is appropriate for use in programs that only deal with dates as it returns the current date without the present time.
Today 属性适用于只处理日期的程序,因为它返回当前日期而不返回当前时间。 I believe what influences this is the time set on your local machine, I doubt that IIS influences this.
我相信影响这个的是你本地机器上设置的时间,我怀疑 IIS 会影响这个。 However, those values can be influenced if you're running your application on a server.
但是,如果您在服务器上运行应用程序,这些值可能会受到影响。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.