To work with local time you need the time zone:
var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
To get the schedule time which you express in local time you need to get the current local time and decide when to schedule next:
var estNow = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, timeZoneInfo);
var estScheduleAt = estNow.Date + TimeSpan.FromHours(2);
Now estScheduleAt
will be 2 AM today in EST. If this code executes between 12 AM and 2 AM EST you should schedule the next action at this time. However, if the current time currently is after 2 AM EST you should schedule at 2 AM tomorrow (still in EST) so you need to add a day to the time:
if (estScheduleAt <= estNow)
estScheduleAt += TimeSpan.FromDays(1);
This algorithm may fail if it run just before 2 AM EST because the predicate estScheduleAt <= estNow
is true but before the action is scheduled it is in the past. You may need to add some additional handling for this situation.
Before scheduling the action you should convert back to UTC to avoid any time zone problems:
var scheduleAt = TimeZoneInfo.ConvertTimeToUtc(estScheduleAt, timeZoneInfo);
Now you can use the scheduleAt
timestamp with your scheduler to make your action run at next 2 AM EST.