Quartz.net 提供了获取下一次触发事件的下一次时间的方法:http://quartznet.sourceforge.net/apidoc/1.0/html/html/cc03bb79-c0c4-6d84-3d05-a17f59727c98.htm
文档声称此 Trigger.GetNextFireTimeUtc()
方法返回一个 DateTime?
但它实际上返回一个 DateTimeOffset?
。我真的不明白 DateTimeOffset
的用途或为什么此函数返回一个而不是常规的 DateTime
。我想要的只是下一次触发器运行但在我的时区内。
我做了这个 trigger.GetNextFireTimeUtc().Value.DateTime
但它给了我一个提前 2 小时的时间,即 UTC 时间。我怎样才能根据我的电脑得到正确的时间?
最佳答案
您可以只使用 DateTimeOffset.LocalDateTime
属性:
trigger.GetNextFireTimeUtc().Value.LocalDateTime
来自文档:
If necessary, the LocalDateTime property converts the current DateTimeOffset object's date and time to the local system's date and time. The conversion is a two-step operation:
- The property converts the current DateTimeOffset object's time to Coordinated Universal Time (UTC).
- The property then converts UTC to local time.
不过,您确实应该研究一下 DateTimeOffset
- 如果您将 BCL 用于日期/时间工作,这是一种需要了解的重要类型。
关于c# - 如何将 UTC DateTimeOffset 转换为使用系统时区的 DateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26296324/