David Geismar David Geismar - 4 years ago 88
Ruby Question

Rails cant save correct date into database

I have a date format like this "18/12/15" as input (it is a string)

18 => day
12 => month
15 => year (2015)

I am trying to store it in my db in a date column but it doesnt save the proper date (for the previous exemple I get Sat, 15 Dec 2018 whereas I should get something like day, 18 Dec 2015.)

How can I fix that

Answer Source

You could use strptime:

require 'date'
Date.strptime('18/12/15', '%d/%m/%y')
#<Date: 2015-12-18 ((2457375j,0s,0n),+0s,2299161j)>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download