locked
Convert Date UTC to Local Time RRS feed

  • Question

  • I have a date that is being given to me in an XML. I need to convert this date to a DateTime object so I can get some information about it, I just have no idea how to convert it. The date looks like this "2011-05-06T08:00:00-04:00". Looking at the date object I know it is the Year-Month-Day Hour:Minute:Second then GMT offset. Is there a way, native to .Net, that I can convert this to a DateTime object without having to parse each section out and then do the offset math myself?

    Thanks.

    Friday, May 6, 2011 7:26 PM

Answers

  • try http://msdn.microsoft.com/en-us/library/bb351654.aspx
    • Marked as answer by Matt Wise Monday, May 9, 2011 1:57 PM
    Saturday, May 7, 2011 4:04 PM

All replies

  • DateTime newTime = DateTime.Parse("2011-05-06T08:00:00-04:00");
    • Proposed as answer by Ali Hamdar Friday, May 6, 2011 8:12 PM
    Friday, May 6, 2011 7:41 PM
  • If I do the above, it returns 05/06/2011 6:00:00 AM.... It's not using the GMT Offset value...
    Saturday, May 7, 2011 12:52 AM
  • try http://msdn.microsoft.com/en-us/library/bb351654.aspx
    • Marked as answer by Matt Wise Monday, May 9, 2011 1:57 PM
    Saturday, May 7, 2011 4:04 PM
  • Any update Matt? Would you mind letting us know how it goes now? Was your question solved?

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, May 9, 2011 7:50 AM
  • Maybe you can clarify a little better what you're looking for. The Parse method took the string and converted it to local time. If you want the UTC time in a DateTime object, you could look at the SpecifyKind or ToUniversalTime methods.

    Monday, May 9, 2011 1:58 PM
  • Thanks for that. This is what worked:

     

    sRawDate = "2011-05-09T06:00:00-04:00";
    DateTimeOffset offsetDate = DateTimeOffset.Parse(sRawDate);
    
    Monday, May 9, 2011 1:58 PM