Ramy
Ramy

Reputation: 21261

date format conversion ruby

I think this says it all:

irb(main):014:0> DateTime.strptime("4/23/1967", "%m/%d/%y").to_s
=> "2019-04-23T00:00:00+00:00"

2019 comes up as the year?

i have a couple of different date formats:

4/23/1967

3-3-1985

I'd like to standardize them both to display in the first format (with slashes).

Upvotes: 0

Views: 840

Answers (2)

steenslag
steenslag

Reputation: 80065

%y takes the year in 2 digits, so 19. Then DateTime assumes it's 2019. You need %Y.

Upvotes: 3

Gareth McCaughan
Gareth McCaughan

Reputation: 19971

Your problem is that you've written %y where you meant %Y. Date format strings are case-sensitive.

irb(main):014:0> DateTime.strptime("4/23/1967", "%m/%d/%y").to_s
=> "2019-04-23T00:00:00+00:00"

but

irb(main):014:0> DateTime.strptime("4/23/1967", "%m/%d/%Y").to_s
=> "1967-04-23T00:00:00+00:00"

Upvotes: 3

Related Questions