How to convert DateTime to Eastern Time

前端 未结 3 2050
走了就别回头了
走了就别回头了 2020-12-24 10:09

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         


        
3条回答
  •  死守一世寂寞
    2020-12-24 10:56

    You need to split up the logic into two;

    • Check if date is more than start date, startTime > now
    • Check if date is less than end date, endTime < now

    For a date range the logic should satisfy both (with logical AND, &&).

    DateTime startTime = DateTime.Today.AddHours(9).AddMinutes(30);
    DateTime endTime = DateTime.Today.AddHours(12+4);
    DateTime now = DateTime.Now;
    if(startTime > now && endTime < now) {
        // do something
    }
    

    If you're in ET timezone it should work fine, but otherwise you need to do some timezone manipulation. Check the other answers.

提交回复
热议问题