Suppose we need to get a fixed time such as 1600 eastern Time in local-time. I have found the following works fine
Dim dtNow As DateTime = DateTime.Now
Dim NYSEclose As DateTime
Dim NYSEclose_localTime As DateTime
NYSEclose = New DateTime(dtNow.Year, dtNow.Month, dtNow.Day, 16, 0, 0)
Dim easternZone As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")
Dim utcTime As DateTime = TimeZoneInfo.ConvertTimeToUtc(NYSEclose, easternZone)
NYSEclose_localTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, TimeZoneInfo.Local)
Next, for testing purposes I would like to simulate the target time as one minute from now so expand the 4th line to
If Debugger.IsAttached Then
'NYSEclose = dtNow.AddMinutes(181) 'local Now is 180min behind ET plus 1
Else
NYSEclose = New DateTime(dtNow.Year, dtNow.Month, dtNow.Day, 16, 0, 0)
endif
TWO questions:
If I use the NEW construction method
NYSEclose = New DateTime(dtNow.Year, dtNow.Month, dtNow.Day, dtNow.hour+3, dtNow.minute+1, dtNow.second)
there is no error well but then one can't just add 3 hours and one minute unless I check all the way to the month to make sure it doesn't spill into the higher tier.
Define explicitely the DateTime.Kind of the NYSEClose like this
Dim c As DateTimeKind = DateTimeKind.Unspecified
Dim b As Date = System.DateTime.SpecifyKind(Now, c)
Dim easternZone As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")
Dim utcTime As DateTime = TimeZoneInfo.ConvertTimeToUtc(b, easternZone)