Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Core and Standard Library Ruby Standard Library: Part 1 The Ruby Date Class

Nick Vitsinsky
Nick Vitsinsky
7,246 Points

Unexpected return when changing format for return Date in irb

I assumed that it doesn't matter what date format I'll try to use as long as it would be valid and and have tried to return the date in a format %d/%m/%Y for

Date.strptime("19/10/1985", "%d/%m/%Y")                          

the output is

=> #<Date: 1985-10-19 ((2446358j,0s,0n),+0s,2299161j)>    

expected return is

=> #<Date: 1985-19-10....

when I try to return the same result for 1/10/1985, I've got expected result

Date.strptime("19/10/1985", "%d/%m/%Y")                                                       
=> #<Date: 1985-10-19 ((2446358j,0s,0n),+0s,2299161j)>  

So what is the problem?

1 Answer

Todd MacIntyre
Todd MacIntyre
12,248 Points

That output is correct. You are expecting the incorrect output to be displayed.

Date.strptime("19/10/1985", "%d/%m/%Y")

You have Year = 1985. Month = 10. Day = 19. Ruby output is displayed in YEAR/MONTH/DAY by default, thus giving 1985-10-19