Jeremy Thomas
Jeremy Thomas

Reputation: 6684

Rails 4: m/d/yy will not successfully format

I am pretty familiar with strptime in Rails, but I cannot get this date to format. The date is coming from a long .csv file so it would be a pain to pre-format the dates differently.

date = "2/9/17"
Date.strptime(date, "%-m/%-d/%y")

ArgumentError: invalid date

Upvotes: 0

Views: 28

Answers (2)

matthewd
matthewd

Reputation: 4435

From the strptime documentation:

strptime does not support specification of flags and width unlike strftime

So you need a slightly simplified pattern:

date = "2/9/17"
Date.strptime(date, "%m/%d/%y")
# => #<Date: 2017-02-09 ((2457794j,0s,0n),+0s,2299161j)>

Upvotes: 2

Sander Garretsen
Sander Garretsen

Reputation: 1723

Date.strptime(date, "%m/%d/%y")?

Upvotes: 0

Related Questions