Rails: How to Make Date Strftime Aware of the Default Locale

Rails: How to make Date strftime aware of the default locale?

Use the l (alias for localize) method instead of raw strftime, like this:

l(date, format: '%B %d, in the year %Y')

See here for more information, hope that helps.

You can also define 'named' formats, a couple of them (short, long) are already predefined.

I18n localize Activerecord Model value Date strftime On Rails console

According to the rails guide date should not be localized with the #translate method (I18n.t) but with the #localize method (I18n.l).

I also recommend you to use the rails-i18n gem which contain common translations such as month names for instance.

Let me know if you still can't manage to find a solution.

Converting @project.end_date.strftime(%b %Y) to defined locale

The simplest way to do this is to use Rails' #l helper method like so:

l @project.end_date

You can also use a format you defined in locales .yml file:

l @project.end_date, format: :short

# config/locales/pirate.yml
pirate:
time:
formats:
short: "arrrround %H'ish"

The Rails guide covers this. Hope that helps!

Ruby/Rails - Convert Date/Time back into Natural Language (2011-02-17 = February 17th, 2011)

Take a look at strftime, this is a link to it's implementation for the Time class.

With it you should be able to get the date to do anything you want :-)

>> Time.now.strftime("%A, %B %d, %Y at %l%p")
=> "Thursday, February 17, 2011 at 5PM"

Changing the default date and time format in Rails 4

Found a nice approach through the Rails Internationalization (I18n) API

Data and time formats can be 'translated' by adding the format to the i18n configuration.

config/locales/en.yml

en:
date:
formats:
default: "%d/%m/%Y"
time:
formats:
default: "%d/%m/%Y %H:%M"

Note: remember to not have tabs for the indent, like I did first time :)


As mentioned by NoelProf in the comments

To use i18n conversion don't forget the l (lower case L) before your
date in views! For example: <%= l your_date %>

You are invited to comment if you found other ways working well.

Rails i18n.default_locale and date format ... doesn't seem to be acting like a default

I had the same problem and this gem helped me https://github.com/jeremyevans/ruby-american_date

Just add it to your Gemfile, no settings needed

Rails 3: How to format a Date in other than English languages?

There is the Internationalization API in Rails that explains how to do that. See for example section 3.2 Adding Date/Time Formats.

<p><%= l Date.today, :format => :short %></p>

You can then add for your locale (russian?) a file ru.yml and include there the definition for your format.

# config/locales/ru.yml
ru:
date:
formats:
short: "%m/%d/%Y"

See the section 4.4 Setting and Passing a Locale how to set the locale.

Working with datetime in Rails

Time in String format:

post.created_at.strftime("FORMAT STRING HERE")
# Without the influence of time-zone: I take it that you want UTC
post.created_at.utc.strftime("FORMAT STRING HERE")

Link for the strftime documentation:
http://ruby-doc.org/core/classes/Time.html#method-i-strftime

For getting the hour, minute and second values:

post.created_at.hour
post.created_at.min
post.created_at.sec

Why does to_date in my view output date in a different format than to_date in Rails console for the same object?

The best way to display time/date in rails views is using I18n localization feature.
You can set datetime formats in your config/locales directory in yaml file. Something like this

# config/locales/datetime.en.yml
en:
time:
formats:
# Use the strftime parameters for formats.
# When no format has been given, it uses default.
# You can provide other formats here if you like!
default: "%d.%m.%Y"
short: "%d %b"
long: "%d %B %Y"
date: "%a, %d %b %Y"

Then use l helper in your views

<%= l client.last_contact, :format => :date %>
  • Rails guides: http://guides.rubyonrails.org/i18n.html#adding-date-time-formats
  • Possible formats: https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/en.yml
  • Datetime in ruby: http://www.dzone.com/snippets/date-time-format-ruby


Related Topics



Leave a reply



Submit