1

Apologies if the title is not clear, I' not really sure how to explain this clearly.

In a Rails app I'm pulling in users' data from a 3rd party provider's API. One of the attributes is the user's birthday.

I am storing the hashed data in my database and, in certain circumstances, need to display the user's birthday in the view.

The problem is that the birthday is not formatted as a Date. Within the hash, it is in the format mm/dd/yyyy. This means that my usual date scoped formats don't work on it.

Given that I am extracting the birthday from the hashed data column as follows

<%= @user.hashed_data["info"]["birthday"] %>

what is the best/ most efficient way to handle this so that I can display localized date formats?

Do I need to split on the / symbols, and then recombine the resulting string to a date? Or am I overcomplicating this?

Thanks for any suggestions.

Andy Harvey
  • 12,333
  • 17
  • 93
  • 185
  • Would you / could you consider converting the string to a proper date at the point of receiving it from the 3rd party API and storing it in your DB as such? – Russell May 08 '12 at 10:12
  • would consider it, but I'm storing the complete token, so may be rather complicated to extract this attribute, manipulate it, the put it back. jdoe's solution does the trick. – Andy Harvey May 08 '12 at 12:29

2 Answers2

1

Try:

Date.strptime('01/31/2011', '%m/%d/%Y') 
# => #<Date: 2011-01-31 (4911185/2,0,2299161)>
jdoe
  • 15,665
  • 2
  • 46
  • 48
0

The simplest way to convert plain text to date or datetime is to use the chronic gem.

A very usefull parameter that you can pass to the chronic parser is the :endian_precedence . See my answer here for more details.

Regards, Dorian

Community
  • 1
  • 1
Dorian
  • 2,571
  • 23
  • 33