Results 1 to 6 of 6

Thread: UTC datetime format

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2008
    Posts
    31

    UTC datetime format

    there must be a better way....

    I need to write a datetime to an xml file in the UTC format:

    "2008-04-12T14:43:11+00:00"

    Obviously i can declare it in DateTime utc, but is there a simple way of making it a string in the above format without having to specify a whole load of arguments as in .tostring("YYYY"-"MM"-"DD" etcetcetcetc)

  2. #2
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: UTC datetime format

    Code:
    String.Format("{0:yyyy-MM-dd}T{0:HH:mm:ss}+00:00", myDate)
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: UTC datetime format

    You just have to call the date object's ToString method and pass in the correct custom format string. Something like this:
    Code:
    Dim d As Date = Date.Now
    MessageBox.Show(d.ToString("yyyy-MM-dd'T'HH:mm:sszzz"))
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  4. #4
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: UTC datetime format

    Nice tricks in that formatting stanav. I didn't know you could do some of that.

    EDIT: Man, you're like the fourth post now that I was impressed by something and clicked "Rate" only to give me a "Spread the love" message.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: UTC datetime format

    You can also use the "O" or "o" standard format format string if you don't mind the partial seconds being included too.

    MSDN lists all the standard and custom format strings for dates, times and numbers. That's always the first place you should look.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Junior Member
    Join Date
    May 2008
    Posts
    31

    Re: UTC datetime format

    thanks guys!

    thats exactly the sort of neat time saving answer I was hoping for!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width