50

I find it hard to understand how UTC works.

I have to do the following but I'm still confused if I'd get the right result.

Objectives:

  1. Ensure all saved dates in Database are in UTC format
  2. Update DefaultTimezone is in Manila time
  3. Ensure all returned dates are in Manila Time

So the code is:

public ConvertDate(DateTime? dateTime)
{
    if (dateTime != null)
    {
        Value = (DateTime)dateTime;
        TimeZone = GetFromConfig.DefaultTimeZone(); 
    }
}


public ConvertDate(DateTime? dateTime, int GMTTimeZone)
{
    if (dateTime != null)
    {
        Value = (DateTime)dateTime;
        TimeZone = GMTTimeZone;
    }
}


public int TimeZone
{
    get { return m_TimeZone; }
    set { m_TimeZone = value; }
}


DateTime m_Value;
public DateTime Value
{
    get { return m_Value; }
    set 
    { 
        m_Value = value;
        DateTime converted = m_Value.ToUniversalTime().ToLocalTime();
    }
}

Sample usage:

DateTime SampleInputFromUser = new DateTime(2012, 1, 22);
ConvertDate newConversion = new ConvertDate(SampleInputFromUser, 21);
DateTime answer = newConversion.Value;

Now I get confused for 'TimeZone'. I don't know how to use it to get the objectives.
Hope you understand my question and have the idea to get the objectives done.

Edit

According to @raveturned answer, I get this following code:

***Added in ConvertDate method

TimeZoneInfo timeInfo = TimeZoneInfo.FindSystemTimeZoneById(GetFromConfig.ManilaTimeZoneKey());
ManilaTime = TimeZoneInfo.ConvertTime(dateTime.Value, TimeZoneInfo.Local, timeInfo).ToUniversalTime();

**New Property

DateTime _ManilaTime;
public DateTime ManilaTime
{
    get { return _ManilaTime; }
    set { _ManilaTime = value; }
}
fiberOptics
  • 6,955
  • 25
  • 70
  • 105
  • Possible duplicate of http://stackoverflow.com/questions/246498/creating-a-datetime-in-a-specific-time-zone-in-c-sharp-fx-3-5 – Developer Mar 26 '12 at 09:04

5 Answers5

98

The .NET framework already has classes and methods available to convert DateTimes between different time zones. Have a look at the ConvertTime methods of the TimeZoneInfo class.

Edit: When you get the time to put into the database, assuming it was created with correct time zone information you can easily convert to UTC:

DateTime utcTime = inputDateTime.ToUniversalTime();

Get timeInfo as done in the question edit:

TimeZoneInfo timeInfo = TimeZoneInfo.FindSystemTimeZoneById(GetFromConfig.ManilaTimeZoneKey());

When you send the database time to user, convert it to the correct timezone using timeInfo.

DateTime userTime = TimeZoneInfo.ConvertTimeFromUtc(dbDateTime, timeInfo);

Personally I'd try and keep this logic separate from the propery get/set methods.

Michael Freidgeim
  • 26,542
  • 16
  • 152
  • 170
raveturned
  • 2,637
  • 24
  • 30
9
var date = System.TimeZoneInfo.ConvertTimeFromUtc(
    DateTime.UtcNow, 
    TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"));
Phoeson
  • 191
  • 1
  • 6
  • 4
    While this code may answer the authors question, it lacks an explanation. Raw snippets are not helpful. Check out the help page for more on [how to write a good answer](https://stackoverflow.com/help/how-to-answer). – sbolel Jan 23 '19 at 08:37
5
TimeZoneInfo infotime = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time (Mexico)");
DateTime thisDate = TimeZoneInfo.ConvertTimeFromUtc(datetimeFromBD, infotime);
LarsTech
  • 80,625
  • 14
  • 153
  • 225
Roger Tello
  • 59
  • 1
  • 3
2

To help others:

    static void ChangeTimezone()
    {
        // Timezone string here:
        foreach (TimeZoneInfo z in TimeZoneInfo.GetSystemTimeZones())
            Console.WriteLine(z.Id);

        // Use one of those timezone strings
        DateTime localDt = DateTime.Today;
        DateTime utcTime = localDt.ToUniversalTime();
        TimeZoneInfo timeInfo = TimeZoneInfo.FindSystemTimeZoneById("US Eastern Standard Time");
        DateTime estDt = TimeZoneInfo.ConvertTimeFromUtc(utcTime, timeInfo);
        return;
    }
user3761555
  • 851
  • 10
  • 21
0

For anyone facing problem in getting TimeZoneInfo in cross-platform (different time zone ids between Windows and Linux), .NET 6 addresses this issue:

Starting with this release, the TimeZoneInfo.FindSystemTimeZoneById method will automatically convert its input to the opposite format if the requested time zone is not found on the system. That means that you can now use either IANA or Windows time zone IDs on any operating system that has time zone data installed*. It uses the same CLDR mappings, but gets them through .NET’s ICU globalization support, so you don’t have to use a separate library.

A brief example:

// Both of these will now work on any supported OS where ICU and time zone data are available.
TimeZoneInfo tzi1 = TimeZoneInfo.FindSystemTimeZoneById("AUS Eastern Standard Time");
TimeZoneInfo tzi2 = TimeZoneInfo.FindSystemTimeZoneById("Australia/Sydney");

Find more info here

And as mentioned in other answers: to get DateTime in the desired timezone from UTC, use TimeZoneInfo.ConvertTimeFromUtc(DateTime, TimeZoneInfo) Method

Meer
  • 678
  • 6
  • 12