Jack Franklin
Jack Franklin

Reputation: 3765

Parsing Twitter & Facebook dates in Ruby

I'm writing a little Rails app & part of that is pulling in a few items from a user's Facebook & Twitter stream. I wish to merge them & then order them in date order.

However, Twitter gives me a date of the format:

Sat Mar 31 17:06:21 +0000 2012

And Facebook gives me the date as:

2012-03-31T16:51:36+0000

So the question is, in Ruby, how can I standardise those dates into the same format so I can then sort them?

I've tried doing Date.parse() but that seems to just return 2012-31-3 which isn't much use as I need the time as well so I can sort them. I have a feeling I might be missing something relatively straight forward.

Thanks,

Jack,

Upvotes: 1

Views: 455

Answers (1)

Michael Kohl
Michael Kohl

Reputation: 66837

Try DateTime.parse or DateTime.strptime.

DateTime.parse("Sat Mar 31 17:06:21 +0000 2012")
#=> #<DateTime: 2012-03-31T17:06:21+00:00 ((2456018j,61581s,0n),+0s,2299161j)>

DateTime.strptime("Sat Mar 31 17:06:21 +0000 2012", "%a %b %d %H:%M:%S %z %Y")
#=> #<DateTime: 2012-03-31T17:06:21+00:00 ((2456018j,61581s,0n),+0s,2299161j)>

Upvotes: 3

Related Questions