views:

232

answers:

1

Hi folks,

How can I convert a DateTimeOffset.Now into a twitter-compatible date/time?

Twitter example:

<created_at>Tue Apr 07 22:52:51 +0000 2009</created_at>

cheers :)

A: 

This nearly does is:

DateTimeOffset now = DateTimeOffset.Now;        
string x = now.ToString("ddd MMM dd HH:mm:ss zzzz yyyy",
                        CultureInfo.InvariantCulture);
Console.WriteLine(x);

... but it ends up with a colon in the time zone bit. I'm looking at removing that now.

EDIT: Blech. The best I can do at the moment is this:

    DateTimeOffset now = DateTimeOffset.Now;        
    string x = now.ToString("ddd MMM dd HH:mm:ss",
                            CultureInfo.InvariantCulture)
        + (now.ToString(" zzzz yyyy", CultureInfo.InvariantCulture)
              .Replace(":", ""));
    Console.WriteLine(x);

That's incredibly ugly. Mind you, this is a really ugly date and time format. Does Twitter really not have a more sensible format you can use?

Jon Skeet
Cheers Jon. I'm also assuming that DateTimeOffset.TryParse( .. ) will work to read that in?
Pure.Krome
@Pure.Chrome: Having added the nastiness to remove the colon, I don't know the best way of parsing... you may want to manually add it in again! As I say, it's probably worth checking if there's a more sensible format you can use.
Jon Skeet
@Jon i got the twitter info from their DEV api like here: http://dev.twitter.com/doc/get/statuses/show
Pure.Krome
@Pure.Krome: Yeah... I just wonder whether there's any way of getting them to give you the data in a saner format. That's a really, really odd format to use.
Jon Skeet
@Jon Agreed :) i was hoping to use that linux net time format in ticks or secs or something ...
Pure.Krome
@Pure.Krome: There are plenty of human-readable date/time formats with standard representations. We don't need to go down to ticks - but putting the year *after* the time zone offset?
Jon Skeet