如何在不更改机器时区和编写时区转换代码的情况下获取 EST 日期时间?
How to get EST date time without changing Machine Time Zone and writing timezone conversion code?
我的机器时区是 IST,因此当我在 C# 中检查 DateTime.Now
时,它将始终显示该时区的当前日期时间。当我执行 DateTime.Now
时,我必须在 EST 中获取日期时间。我不想更改机器的时区,也不想使用时区 ID 将当前日期时间转换为 EST 编写时间转换代码。
原因是我在文章的某处读到,当您更改当前文化时,系统会以这种方式运行,所以 DateTime.Now
也应该给我 EST 日期。如果我错了,请纠正我。
The reason is I read somewhere in article that when you change currentculture the system would behave that way so DateTime.Now also should give me date in EST. Please correct me if I am wrong.
好的,我会纠正你的。你错了(对不起)。
DateTime.Now
始终 returns 本地时间基于操作系统的本地时区设置。它不受 CurrentCulture
更改的影响。更改当前文化会影响从 DateTime
创建字符串时使用的显示格式和日历系统,但不会影响 DateTime
本身,因此也不会影响时区。
通常,文化或区域设置与时区正交。例如,如果我是一个说英语的美国人,暂时访问日本,我的文化可能是 en-US
,我的时区可能是 Tokyo Standard Time
。
我的机器时区是 IST,因此当我在 C# 中检查 DateTime.Now
时,它将始终显示该时区的当前日期时间。当我执行 DateTime.Now
时,我必须在 EST 中获取日期时间。我不想更改机器的时区,也不想使用时区 ID 将当前日期时间转换为 EST 编写时间转换代码。
原因是我在文章的某处读到,当您更改当前文化时,系统会以这种方式运行,所以 DateTime.Now
也应该给我 EST 日期。如果我错了,请纠正我。
The reason is I read somewhere in article that when you change currentculture the system would behave that way so DateTime.Now also should give me date in EST. Please correct me if I am wrong.
好的,我会纠正你的。你错了(对不起)。
DateTime.Now
始终 returns 本地时间基于操作系统的本地时区设置。它不受 CurrentCulture
更改的影响。更改当前文化会影响从 DateTime
创建字符串时使用的显示格式和日历系统,但不会影响 DateTime
本身,因此也不会影响时区。
通常,文化或区域设置与时区正交。例如,如果我是一个说英语的美国人,暂时访问日本,我的文化可能是 en-US
,我的时区可能是 Tokyo Standard Time
。