locked
Convert UTC to Pacific Standard Time RRS feed

  • Question

  • User265788195 posted

    I need to write an extension method to convert UTC time to Pacific Standard Time considering daylight savings time in C#. 

    How should I start? Never done any extension methods.

    I need an extension on DataTimeOffset? and also DateTimeOffset kind of variables.

    Any help?

    Monday, October 13, 2014 4:46 PM

Answers

  • User-434868552 posted

    @nissan  

    Console.WriteLine (DateTime.Now.IsDaylightSavingTime());
    Console.WriteLine (DateTime.Now.ToString());
    Console.WriteLine (DateTime.Now.ToLocalTime());
    Console.WriteLine (DateTime.Now.ToUniversalTime());    // currently 4 hours (EDST) Eastern Daylight Saving Time
    Console.WriteLine ( DateTime.Now.ToLongDateString());
    Console.WriteLine (System.TimeZoneInfo.Local.ToString());   // find your local time and offset

    output:

    True
    2014-10-13 18:05:21
    2014-10-13 18:05:21
    2014-10-13 22:05:21
    2014 October 13 Monday
    (UTC-05:00) Eastern Time (US & Canada)

    what does -8:00 mean?

    it is the difference between UTC and not UTC based on your system settings.

    it is important to know whether Daylight Saving Time is being taken into consideration.

    when Daylight Saving Time is not being taken into consideration, the Pacific Standard Time is 8 hours behind UTC.

    so I have DateTimeOffset?  myVal = pointData.GetMyStartDate();

    Now what should I do? myVal.? something here?

    ???  what is your code for your .GetMyStartDate() method; what is the type of your pointData object?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 13, 2014 6:16 PM

All replies

  • User-434868552 posted

    @nissan  

    is  there some reason why you do not wish to use the .NET Framework conversion methods from the TimeZoneInfo Class?

    Reference:  http://msdn.microsoft.com/en-us/library/system.timezoneinfo%28v=vs.110%29.aspx "TimeZoneInfo Class"

    Monday, October 13, 2014 5:29 PM
  • User265788195 posted

    So how do I use that.. First of all I am confused if my data is already in PST or UTC.

    I have the data like this 2014-01-29 12:25:00.0000000 -08:00; what does -8:00 mean? is it in local time?

    so I have DateTimeOffset?  myVal = pointData.GetMyStartDate();

    Now what should I do? myVal.? something here?

    Monday, October 13, 2014 5:46 PM
  • User-434868552 posted

    @nissan  

    Console.WriteLine (DateTime.Now.IsDaylightSavingTime());
    Console.WriteLine (DateTime.Now.ToString());
    Console.WriteLine (DateTime.Now.ToLocalTime());
    Console.WriteLine (DateTime.Now.ToUniversalTime());    // currently 4 hours (EDST) Eastern Daylight Saving Time
    Console.WriteLine ( DateTime.Now.ToLongDateString());
    Console.WriteLine (System.TimeZoneInfo.Local.ToString());   // find your local time and offset

    output:

    True
    2014-10-13 18:05:21
    2014-10-13 18:05:21
    2014-10-13 22:05:21
    2014 October 13 Monday
    (UTC-05:00) Eastern Time (US & Canada)

    what does -8:00 mean?

    it is the difference between UTC and not UTC based on your system settings.

    it is important to know whether Daylight Saving Time is being taken into consideration.

    when Daylight Saving Time is not being taken into consideration, the Pacific Standard Time is 8 hours behind UTC.

    so I have DateTimeOffset?  myVal = pointData.GetMyStartDate();

    Now what should I do? myVal.? something here?

    ???  what is your code for your .GetMyStartDate() method; what is the type of your pointData object?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 13, 2014 6:16 PM