78

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#?

locoboy
  • 38,002
  • 70
  • 184
  • 260

4 Answers4

177

Try this:

var timeUtc = DateTime.UtcNow;
TimeZoneInfo easternZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
DateTime easternTime = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, easternZone);
Abhi
  • 4,872
  • 1
  • 22
  • 20
Dietpixel
  • 9,983
  • 11
  • 28
  • 33
  • 13
    or anyone else, does this also account for `Eastern Daylight Time`? Or do you have to detect if DST is on, and then specify which zone to use? –  Jan 06 '12 at 16:19
  • 4
    small addition: use DateTime.UtcNow this is what was meant with timeUtc – Michael Bahig Feb 07 '13 at 19:48
  • 11
    `Eastern Standard Time` should handle DST properly. See http://stackoverflow.com/questions/964894/how-to-convert-time-between-timezones-utc-to-edt – csm8118 Jul 23 '13 at 18:51
  • 1
    for the 2nd line, I'd use TimeSpan easternTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, easternZone).TimeOfDay; Then you can easily compare to new TimeSpan(16, 30, 00) etc. : – Guy Dec 11 '14 at 13:52
  • 6
    If you are using dotnet core or monodevelop and are planing to run that code on linux then use `Ameria/New_York` instad of `Eastern Standard TIme`. – Tono Nam Sep 26 '19 at 16:27
  • 5
    @TonoNam `Ameria/New_York` is misspelled (just noting it in case somebody else tries to copy and paste like me). Otherwise, thanks for the comment! – Mark Rucker Apr 03 '20 at 15:18
12

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);
Roman
  • 19,581
  • 6
  • 68
  • 84
4

You can create an extension method for it,

NOTE: This is converting the current DateTime to EST

Like this,

    using System;
    using System.Runtime.InteropServices;

    public static DateTime ConvertToEasternTime(this DateTime value)
    {
        TimeZoneInfo tz = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
            ? TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")
            : TimeZoneInfo.FindSystemTimeZoneById("America/New_York");

        return TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tz);
    }

And you can use it like this,

EasternTime = DateTime.Now.ConvertToEasternTime(),

Just make sure you are importing the namespace to use this method.

NOTE: This below approach is converting the given DateTime to EST

    private static TimeZoneInfo GetEasternTimeZoneInfo()
    {
        TimeZoneInfo tz = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
           ? TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")
           : TimeZoneInfo.FindSystemTimeZoneById("America/New_York");

        return tz;
    }

This above method returns the EST TimeZone.

    public static DateTime ConvertToEasternTime(DateTime date)
    {
        return TimeZoneInfo.ConvertTimeFromUtc(date, GetEasternTimeZoneInfo());
    }

This above method returns the date that you passed in as a parameter to the EST time

To use the above method we can do,

     var convertToEST= DateUtility.ConvertToEasternTime(date);

We need to pass the date as the argument to convert. Sometimes we might have to do,

     var convertToEST= DateUtility.ConvertToEasternTime(date).Date;

Just make sure you are importing the namespace to use this method.

prajun7
  • 133
  • 11
-9

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.

Spoike
  • 119,724
  • 44
  • 140
  • 158