Saving date format (mm / dd / yyyy) does not work

I'm trying to create a user with an expiration date ("05/30/2015" ie "mm / dd / yyyy"), but it returns nil for the expiration date.

u = User.new
=> #<User id: nil, first_name: nil, last_name: nil, email: nil, expiration: nil, remote_id_string: nil, remote_created_at: nil, phone_number: nil, company: nil, created_at: nil, updated_at: nil> 

u.expiration = "05/30/2015"
=> "05/30/2015" 

u.expiration
=> nil

      

But reformatting the format as (yyyy / mm / dd) worked

u.expiration = "2015/05/30"
=> "2015/05/30"

u.expiration
=> Sat, 30 May 2015 

      

I tried to fix it by setting "gem validates_timeliness" and I used "parser.us_use_formats" for the date format "05/30/2015" it still returns zero.

How do I handle the date format so that it doesn't return null and accept that format ("05/30/2015" ie "mm / dd / yyyy")?

+3


source to share


1 answer


You can use this,



u.expiration = Date.strptime("05/30/2015","%m/%d/%Y")
=> "Sat, 30 May 2015"

u.expiration
=> "Sat, 30 May 2015"

      

+3


source







All Articles