How to convert DateTime to Eastern Time
I'm trying to create an application that triggers some code when the financial markets are open. Basically in pseudo code:
if(9:30AM ET < Time.Now < 4:00PM ET) {//do something}
Is there a way I can do this using the DateTime object in C#?
Solution 1:
Try this:
var timeUtc = DateTime.UtcNow;
TimeZoneInfo easternZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
DateTime easternTime = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, easternZone);
Solution 2:
You could probably use the ConvertTime method of the TimeZoneInfo class to convert a given DateTime to the Eastern timezone and do the comparison from there.
var timeToConvert = //whereever you're getting the time from
var est = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
var targetTime = TimeZoneInfo.ConvertTime(timeToConvert, est);