How to Use Us-Style Dates in Rails Using Ruby 1.9

How can I use US-style dates in Rails using Ruby 1.9?

Gem: ruby-american_date

This gem was created since I asked this question. I'm now using it and have been pleased.

https://github.com/jeremyevans/ruby-american_date

How to make US date format as the default when saving to a model in Rails 3

As of Ruby 1.9, Date.parse stopped handling the ambiguous format mm/dd/yyyy (american format) or dd/mm/yyyy (rest of civilized world format).

The american_date gem linked here makes the assumption older Ruby did, and can thus parse an american date as expected.

rails - how to show and save a date in us format?

I changed

/app/assets/javascripts/datepicker.js

changing

var format = 'yy-mm-dd';

to

var format = 'mm/dd/yyyy';

Then I added a file config/inititlaizers/date-format.js, with

# Date
# ----------------------------
Date::DATE_FORMATS[:default] = "%m/%d/%Y"

# DateTime
# ----------------------------
DateTime::DATE_FORMATS[:default] = "%m/%d/%Y"

# Time
# ----------------------------
Time::DATE_FORMATS[:default] = "%m/%d/%Y %H:%M:%S"

This helped in all the displays and input fields and date-picker but the date still flipped.

Finally (this bit fixes the date flipping part), I changed my controller to be:

def update

r = Link.find(params[:id])
r.tap { |link|
link.update!(link_params)
}
r.update!(:content_date => link_params[:content_date].to_date.strftime("%Y-%d-%m"))
redirect_to r

end

How to convert MM/DD/YYYY to time in ruby?

You should be able to send that string right into Time.parse like so:

2.0.0-p247 :001 > require 'time'
=> true

Before Ruby 1.9:

2.0.0-p247 :002 > Time.parse("12/1/2014 5:55PM")
=> 2014-12-01 17:55:00 -0500

After Ruby 1.9

2.0.0-p247 :002 > Time.strptime("12/1/2014 5:55PM","%m/%d/%Y %I:%M%p")
=> 2014-12-01 17:55:00 -0500

Determine encoding in a string

You can try to guess but with a lot of false results. Md5 always have 32 characters, base64 have a limited set of possible characters, etc.



Related Topics



Leave a reply



Submit