UTC DateTime Format

Last post 01-15-2009 9:42 PM by Dil. 6 replies.

Sort Posts:

  • UTC DateTime Format

    01-15-2009, 1:21 PM
    • Member
      702 point Member
    • Dil
    • Member since 06-01-2007, 10:12 PM
    • Posts 112

    Hi All,

     I want to consume a webservice which expects the datetime in following format.

    <SDate>2009-10-02T00:00:00.0000000-08:00</SDate>
     
    To achieve this I am passing the UniversalDateTime by using "sDate.ToUniversalTime()"

    I serialized the ws request object and its showing the datetime like this

    <SDate>2009-10-02T08:00:00Z</SDate> 
     
    Is this correct? Or how can I convert my datetime as per the webservice specification


    Thanks in advance
  • Re: UTC DateTime Format

    01-15-2009, 1:38 PM
    • All-Star
      91,815 point All-Star
    • SGWellens
    • Member since 01-02-2007, 4:27 PM
    • Twin Cities, MN
    • Posts 7,490
    • Moderator
      TrustedFriends-MVPs

    You can try the "Round-Trip" format:

    DateTime.Now.ToString("O");

    Steve Wellens

    My blog
  • Re: UTC DateTime Format

    01-15-2009, 3:02 PM
    • Member
      702 point Member
    • Dil
    • Member since 06-01-2007, 10:12 PM
    • Posts 112

     
    Thanks for your reply Wellens,  Your solution is working fine with current date and time

    my scenario is

    The web application server is located in Chicago and am calling this webservice located in Los Angeles (PST) .

    So how can I manually create a DateTime with the specified format. (-8) ?

    for eg  convert "10-02-2009" to  "2009-10-02T00:00:00.0000000-08:00"

  • Re: UTC DateTime Format

    01-15-2009, 7:01 PM
    • All-Star
      91,815 point All-Star
    • SGWellens
    • Member since 01-02-2007, 4:27 PM
    • Twin Cities, MN
    • Posts 7,490
    • Moderator
      TrustedFriends-MVPs

    If you are only using 10-02-2009 and do not care about the time zone, this should work:

    DateTime MyDate = DateTime.ParseExact("10-02-2009", "MM-dd-yyyy", null);
    MyDate = DateTime.SpecifyKind(MyDate, DateTimeKind.Local);
    String Test = MyDate.ToString("O");
    
    

     

     

    Steve Wellens

    My blog
  • Re: UTC DateTime Format

    01-15-2009, 7:45 PM
    • Member
      702 point Member
    • Dil
    • Member since 06-01-2007, 10:12 PM
    • Posts 112

     Timezone is the issue - As I mentioned in my previous post, my web application will get the "CST" time if I am using DateTimeKind.Local but I need to pass "PST" instead

  • Re: UTC DateTime Format

    01-15-2009, 9:20 PM
    Answer
    • All-Star
      91,815 point All-Star
    • SGWellens
    • Member since 01-02-2007, 4:27 PM
    • Twin Cities, MN
    • Posts 7,490
    • Moderator
      TrustedFriends-MVPs

    Dil:
    Timezone is the issue

    Really?  Because in your example you have a date with no time.  If you are using .Net 3.5 you can do something like:

    DateTime oldTime = new DateTime(2007, 6, 23, 10, 0, 0);
    TimeZoneInfo timeZone1 = TimeZoneInfo.FindSystemTimeZoneById("Mountain Standard Time");
    TimeZoneInfo timeZone2 = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
    DateTime newTime = TimeZoneInfo.ConvertTime(oldTime, timeZone1, timeZone2);

    Otherwise it's more difficult with Daylight Savings time being the monkeywrench in the works. 

     

    Steve Wellens

    My blog
  • Re: UTC DateTime Format

    01-15-2009, 9:42 PM
    • Member
      702 point Member
    • Dil
    • Member since 06-01-2007, 10:12 PM
    • Posts 112
    Thanks
Page 1 of 1 (7 items)