none
Convert UTC Time To Eastern Time Zone RRS feed

  • Question

  • I am pulling data from an API and it returns the date as a string in datetime format like below:

    2018-10-26T17:39:44.000Z

    I am wanting to convert that to Eastern Time Zone (I believe that is GMT+5)

    And I tried this syntax, but the code hangs up for 60 - 90 seconds when it hits the conversion line of:

    DateTime easternDateTime = TimeZoneInfo.ConvertTimeFromUtc(myDate, easternTimeZone);

    This is full code, is there a way to optimize this so the conversion takes roughly seconds as opposed to over a minute?

    foreach (var item in packageHistory)
    {
    	DateTime myDate = DateTime.Parse(item.CreatedAt);
    	DateTime easternDateTime = TimeZoneInfo.ConvertTimeFromUtc(myDate, easternTimeZone);
    	Console.WriteLine(easternDateTime);
    }

    Saturday, October 27, 2018 12:14 AM

Answers

  • Try this

    var utcString = "2018-10-26T17:39:44.000Z";
    var utcDateTime = DateTime.Parse(utcString,CultureInfo.InvariantCulture,DateTimeStyles.RoundtripKind);
    var easternTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
    
    var easternDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcDateTime,easternTimeZone);
    Console.WriteLine(easternDateTime);


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by IndigoMontoya Saturday, October 27, 2018 9:46 PM
    Saturday, October 27, 2018 9:32 AM
    Moderator

All replies

  • Try this

    var utcString = "2018-10-26T17:39:44.000Z";
    var utcDateTime = DateTime.Parse(utcString,CultureInfo.InvariantCulture,DateTimeStyles.RoundtripKind);
    var easternTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
    
    var easternDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcDateTime,easternTimeZone);
    Console.WriteLine(easternDateTime);


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by IndigoMontoya Saturday, October 27, 2018 9:46 PM
    Saturday, October 27, 2018 9:32 AM
    Moderator
  • Hello Karen, and thanks, again, for resolving a very difficult problem I had several weeks ago.

    re this problem, I have just a follow-up comment for the original poster …

    UTC conversion is, on the surface, a surprisingly complicated task due to the fact that DST is not an international standard. Most, though not all, places, employ some sort of "Standard" & "Daylight Saving" Time scheme. But not all changes occur on the same date, even in the same year; some are not a full hour change (who knew?); and at least in the US, the effective date changes each year.

    Your suggestion above is an excellent pedagogical tool. It shows exactly how to make the appropriate call on the System to achieve the desired result.   (I've come to expect that from your posts!)

    But for a practical solution, Indigo Montoya may need a bit more info … he (or she, as the case may be) needs to know IF THERE IS a switch between Standard Time and Daylight Saving Time; when Standard Time switches to Daylight Saving Time, the 'local time zone' he's trying to work with, and perhaps even the country he's trying to calculate 'local time' for  (for instance, if he's trying to time an event that took place in Denver, CO, or Paris, France, using Eastern Standard Time (EST) is going to produce disappointing results). And even if he's calculating local time for Lincoln MA, I WOULD THINK that using EST will give the wrong answer in, say, August.

    B U T, there's an excellent resource on The 'Net that he can use to get the information he needs …

    https://www.timeanddate.com/time/dst/2018.html
    and
    https://www.timeanddate.com/time/us/daylight-saving-usa.html.

    ( I have NO AFFILIATION with T 'n D, whatsoever, but reference it occasionally for my apps which monitor sensors around the world. They all report in UTC).

    There's an exceptionally important statement on T 'n D which may help Indigo Montoya :

    "The DST periodin the United States begins each year on the 2nd Sunday in March, when clocks are set forward by 1 hour. They are turned back again to standard timeon the 1st Sunday in November as DST ends."

    Armed with that information, he can determine the dates that DST starts and ends, and can use that information to determine the correct local time.

     Hope that resolves some of the mystery of a surprisingly complex issue in actual practice.

    Monday, October 29, 2018 12:02 AM