Ruby strftime in UK format i.e. dd / mm / yy

I know what I can do @something.created_at.strftime('%d/%m/%y')

to get the date in dd / mm / yy format , but this is a pain.

However, American users just need to do a simple: @something.created_at.strftime('%x')

which gives mm / dd / yy .

Is it possible for the installation to %x

work properly, that is, in British style (since we invented time).

+3


source to share


3 answers


The correct way to do it now is:

# in config/locales/en.yml
date:
  formats:
    default:
      "%d/%m/%y"

      

Then in the view:



= l Date.current

      

Yes it. The l method is shorthand for I18n.localize. Read more in the Rails I18n guide .

+1


source


If this is something you intend to use frequently, you can use Proc:



def brit_date
  Proc.new 
end

proc = brit_date {Time.now.strftime("%d/%m/%y")
proc.call
 => "24/11/14"

      

0


source


Take a look at the gem easy_dates

gem install gemcutter
gem tumble
gem install easy_dates

      

you don't have to worry about formatting your dates constantly with strftime .

-1


source







All Articles