【发布时间】:2020-05-14 02:21:42
【问题描述】:
是否可以(相当容易地)在另一个时区的日期中添加一天而不是 DateTime.Local,同时尊重该特定时区的不同调整规则(DST 等)?
var rst = TimeZoneInfo.FindSystemTimeZoneById("Romance Standard Time");
var dayInSpecificTimezone = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, rst); // Got the datetime in specified timezone
// now I would like to add a "day" to that, still observing the rules of that timezone.. something like:
var sameTimeTheDayAfterThat = dayInSpecificTimezone.AddDays(1, rst); // no such method exists
【问题讨论】:
-
这个能回答你的问题吗? stackoverflow.com/questions/47710262/…
-
AddDays确实存在,但它需要一个double参数,并且在这种情况下您要传递两个参数。除非我误解了你的问题。 -
@C4d 谢谢。它几乎做到了,但我需要指定时区的新时间 - 所以如果例如跳过一个小时等,我不能抛出异常。
-
@Charmander 谢谢,是的 - 我知道这是一个虚构的方法 - 我想要一个将双精度和时区信息作为参数的方法..
-
@monkeycsharp 你已经有了你的
dayInSpecificTimezone,它是在一个特定的时区创建的(正如名字所说)。AddDays()不是已经考虑到传递的时区了吗?