3

I have a date format, something similar to:

Mon, Sun, 22 Aug 2010 12:38:33 GMT

How do I convert this to EST format?

Note: I know it can be achieved using TimeZoneinfo, but that is introduced in 3.5, i want to do it 2.0 or any old version.

3
  • Are you afte the timezone time (7:38:33 EST) or changing the date format (08/22/2010)? Commented Aug 23, 2010 at 11:13
  • Sorry, not possible without using 3.5 or designing your own that decodes the current timezone and then subtracts it. Commented Aug 23, 2010 at 11:16
  • Okay, to summarize we're after converting time according to timezone e.g. 1100 GMT to 0600 EST. Some more questions: Is this just to display time to the user? Will we need to take into account daylight saving adjustments and historical times e.g. 1100 GMT can be 0700 EST with daylight saving, but what if it was 1100 GMT when daylight saving didn't apply? Oh and it needs to work on Net 2.0 or 1.1. Commented Aug 26, 2010 at 7:30

4 Answers 4

3

I guess you have to go the old road of understanding timezones. Your example is though pretty easy.

GMT is UTC with daylight saving changes.
EST is UTC -5hrs with daylight saving changes.

So you just have to substract 5 hours to get the time in EST.

Sign up to request clarification or add additional context in comments.

4 Comments

Actually GMT isn't UTC with daylight saving. You'll find that UTC and GMT are the same. Currently the UK is on GMT+1 (British Summer Time).
@Chris - Pff, this is getting confusing... I based what I said on the list of timezones in Windows 7, you can see one listed here, for example: riotingbits.com/2009/… - and in that list GMT is defined as UTC with daylight saving changes. Now, I don't know how consistent the timezone definitions are across different mediums...
I just did a little code test with .NET 3.5, converting DateTime.UtcNow first to "Eastern Standard Time", then to "GMT Standard Time", and I did get a difference of 5 hours. But you're right, Chris, GMT is normally the same as UTC - it seems that Windows (at least Win 7) treats it differently.
You should also mention that in 2007 following from the [The Energy Policy Act of 2005] the United States extended daylight saving time by roughly 2 weeks in both directions, this now means that the time difference between EST and GMT is -4 hours for roughly 4 weeks of the year. It's not as simple as -5 hours anymore.
2

For Net 2.0 and 1.1 you could use the TimeZone class which has now been replaced by the TimeZoneInfo class.

Edit after further research It would appear that prior to NET 3.5 all that was available was the TimeZone class. It is not possible to calculate the TimeDate value for timezones other than the local one and UTC. So if you wish to calculate the time as EST or PST and you're based in the UK then things become a little more difficult.

Here's a short demo program for using TimeZone (its from a basic console app.:

class Program
{
    static void Main(string[] args)
    {
        DateTime time = DateTime.UtcNow;
        Console.WriteLine(string.Format("UTC time is {0}", time.ToShortTimeString()));

        TimeZone zone = TimeZone.CurrentTimeZone;

        //The following line depends on a call to TimeZone.GetUtcOffset for NET 1.0 and 1.1
        DateTime workingTime = zone.ToLocalTime(time);
        DisplayTime(workingTime, zone);
        IFormatProvider culture = new System.Globalization.CultureInfo("en-GB", true);
        workingTime = DateTime.Parse("22/2/2010 12:15:32");

        Console.WriteLine();
        Console.WriteLine(string.Format("Historical Date Time is : {0}",workingTime.ToString()));
        DisplayTime(workingTime, zone);
        Console.WriteLine("Press any key to close ...");
        Console.ReadLine();
    }
    static void DisplayTime(DateTime time, TimeZone zone)
    {
        Console.WriteLine(string.Format("Current time zone is {0}", zone.StandardName));
        Console.WriteLine(string.Format("Does this time include Daylight saving? - {0}", zone.IsDaylightSavingTime(time) ? "Yes" : "No"));
        if (zone.IsDaylightSavingTime(time))
        {
            Console.WriteLine(string.Format("So this time locally is {0} {1}", time.ToShortTimeString(), zone.DaylightName));
        }
        else
        {
            Console.WriteLine(string.Format("So this time locally is {0} {1}", time.ToShortTimeString(), zone.StandardName));
        }
        Console.WriteLine(string.Format("Time offset from UTC is {0} hours.", zone.GetUtcOffset(time)));

    }
}

As you can see it only deals with local time and UTC.

Comments

0

EST is probably not a format but an abbreviation of the time zone. I.e. the "manual" way would be to parse the above date and subtract the time zone difference. You should be beware of daylight saving time periods, i.e. you need to check if the above time fits the DST period or not.

Comments

0

The simplest way would probably be to just take 5 hours off the time like so.

DateTime dateTime = DateTime.UtcNow;
dateTime.AddHours(-5);

But this won't account for daylight savings etc. But you could always code for that possibility.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.