如何在c#中的datetimeoffset中获取明天日期的具体时间?

问题描述 投票:0回答:1

我想要明天日期(中部标准时间上午 7 点)的

DateTimeOffset
值。

我当前的代码是:

var tomorrow = DateTime.Now.AddDays(1);

var tomorrowDate = new DateTime(tomorrow.Year, tomorrow.Month, tomorrow.Day, 07, 00, 00, DateTimeKind.Local);

DateTimeOffset datetimeOffsetInCentralTimeZone = new DateTimeOffset(tomorrowDate, TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time").GetUtcOffset(tomorrowDate));

return datetimeOffsetInCentralTimeZone;

这是正确的吗?有没有更简单的方法来获取 datetimeoffset 值?

c# datetime timezone-offset datetimeoffset
1个回答
0
投票

您可以使用

DateTime.Today.AddDays(1).AddHours(7)
获取实际时间。

var date = DateTime.Today.AddDays(1).AddHours(7);
var tz = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
var datetimeOffsetInCentralTimeZone = new DateTimeOffset(date, tz.GetUtcOffset(date));

请注意,这使用本地时区来确定“今天”。如果您想要 UTC 格式的“今天”,请使用:

var date = DateTime.UtcNow.Date.AddDays(1).AddHours(7);
© www.soinside.com 2019 - 2024. All rights reserved.