Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Convert Twitter Timestamp to DateTime?

I've been googling this for some time now, but I could never find an answer to my problem.

I'm making a Twitter client using C# and Windows Presentation Foundation, and I can't figure out how to change the timestamps that Twitter supplies to a DateTime or UNIX timestamp.

I know it's possible with Regex, but I never found a solution.

Is there some easy way to do this that I'm unaware of? The Twitter timestamp format that I'm trying to convert from looks like this:

Fri Feb 11 23:45:15 +0000 2011

Any ideas?

like image 716
sammarks Avatar asked Feb 11 '11 23:02

sammarks


2 Answers

Based on a sample elsewhere, how about using the ParseExact method:

const string format = "ddd MMM dd HH:mm:ss zzzz yyyy";
my_date = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture);
like image 155
martin clayton Avatar answered Sep 26 '22 18:09

martin clayton


DateTimeOffset timestamp;
if (DateTimeOffset.TryParseExact(
    "Fri Feb 11 23:45:15 +0000 2011",
    "ddd MMM dd HH:mm:ss K yyyy",
    null,
    DateTimeStyles.None,
    out timestamp))
  ; // use timestamp

This assumes:

  • The current culture is the correct one to use (e.g., "Fri", "Feb", etc.)
  • Dates, hours, minutes, and seconds are 0-filled (e.g., "Fri Feb 04 02:05:02 +0000 2011")
like image 30
Stephen Cleary Avatar answered Sep 22 '22 18:09

Stephen Cleary