I want a DateTimeOffset
value for tomorrow's date, 7 AM , Central Standard Time.
My current code is:
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;
Is this correct? Is there an easier way to get the datetimeoffset value?
For this answer I'm making the following assumptions:
DateTimeOffset
in Central Time is required. (This seems to be implied in the question.)DateTimeOffset
will be used for and it's possible the OP wants 7:00 AM CST and 6:00 AM CDT but that would be the less common case.)// Get the TZ. This code is using Central Time but it could be any time zone.
var timeZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
// Get tomorrow in the TZ. (Note that adding a day could also advance the month or the month and year.)
var tomorrow = TimeZoneInfo.ConvertTime(DateTimeOffset.UtcNow, timeZone).AddDays(1);
// Construct a new DateTimeOffset using 'tomorrow' in the TZ and setting the time to 7:00 AM.
var target = new DateTimeOffset(tomorrow.Year, tomorrow.Month, tomorrow.Day, 7, 0, 0, 0, tomorrow.Offset);
If you wanted to adjust the 7:00 AM time during Daylight Saving, the following code can be added to test for Daylight Saving, find the difference between Standard and Daylight (don't assume the difference is 1 hour), and modify the time.
if (timeZone.IsDaylightSavingTime(target))
{
target = target. Add(timeZone.BaseUtcOffset - target.Offset);
}