Simplificator_Logo

web applications


viagra moins cher viagra vendita italia achete levitra trouver du levitra pastilla sildenafil achat de cialis acheter viagra pas chere levitra 20 mg sildenafil sin receta viagra ricetta medica cialis generico 10 mg viagra bestellen levitra venta libre dysfonction erectile viagra 100 mg levitra ohne rezept acquisto viagra comprar viagra em portugal acheter viagra cialis prijs sildenafil kaufen viagra italia acheter cialis pharmacie comprar cialis generico kamagra gel acquisto viagra italia viagra venta libre tadalafil moins cher clomid prix kamagra 100 vardenafil generique sildenafil costo acheter cialis sur internet achat viagra en ligne cialis receta pastilla levitra cialis belgique compro viagra sildenafil rezeptfrei levitra en pharmacie levitra generique cialis meilleur prix acheter cialis generique pharmacie en ligne curare impotenza comprare levitra leivtra moins cher acheter prozac viagra senza ricetta acheter cialis sur la net acheter isotretinoine levitra generico cialis generico vardenafil generika levitra ricetta achat de viagra cialis venta libre levitra sin receta cialis suisse cialis svizzera acheter cialis en belgique sildenafil precio cialis preço posologia viagra viagra sans prescription cialis bon prix viagra kopen prezzi viagra comprar cialis tadalafil bestellen viagra prijs cialis livraison rapide viagra te koop levitra france cialis prix propecia prix kamagra te koop prezzi cialis compro levitra acheter zithromax commande viagra viagra ricetta receta viagra acheter cialis en ligne prezzi levitra cialis vente en ligne sildenafil moins cher commander kamagra impuissance homme acquistare viagra levitra sur le net compra levitra levitra pharmacie vendo viagra kamagra en france acquisto viagra originale achat levitra acheter kamagra france cialis sur le net pildoras cialis cura impotenza cialis france acquista levitra cialis rezeptfrei levitra precio cialis moins cher kamagra rezeptfrei levitra prezzo propecia generique achete cialis generique du cialis acheter kamagra oral jelly trouver du cialis vente de cialis traitement impuissance viagra rezeptfrei cialis sur internet vendo levitra vendita cialis aquisto viagra achat pharmacie acquisto viagra senza ricetta viagra en ligne cialis europe compro cialis cialis inde levitra te koop impuissance erection aquisto cialis acheter zyban viagra donne compra viagra
« Some Netbeans Shortcuts
RESTful In-place form editing »

Custom Date/Time formats in Ruby (on Rails)

In Switzerland we use a different formatting for date/time than in the USA (in fact most of Europe uses different formatting than the USA). Ruby (on Rails) is very centred on US coders and US applications and this can sometimes be a source of confusion and frustration. So let’s add some custom formats to Rails so we can not only to Date.today.to_s(:short) but also Date.today.to_s(:my_custom_date_format)

Rails has a built in set of formats which is stored in ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS and ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS and all you have to do is to add your own formats to this hash.
In config/initializers create a file date_formats.rb. This file we can use to register custom formats withouth messing up our environment.rb. If you need this often you could also extract it to a plugin so you can “install” your custom dateformats everywhere with a single script/install. In this file you can now merge your own formats to the existing ones or change existing formats:

ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(:my_format => ‘%d. %m. %Y’)

The expressions you can use for formatting can be found in the source of Date.strftime. Or with this little piece of code:

>> (’a’..’z').each do |c|
?> expression = “%#{c}”
>> puts “#{expression} -> #{Date.today.strftime(expression)} / #{expression.upcase} -> #{Date.today.strftime(expression.upcase)}”
>> end
%a -> Sun / %A -> Sunday
%b -> Apr / %B -> April
%c -> Sun Apr 13 00:00:00 2008 / %C -> 20
%d -> 13 / %D -> 04/13/08
%e -> 13 / %E -> E
%f -> f / %F -> 2008-04-13
%g -> 08 / %G -> 2008
%h -> Apr / %H -> 00
%i -> i / %I -> 12
%j -> 104 / %J -> J
%k -> 0 / %K -> K
%l -> 12 / %L -> L
%m -> 04 / %M -> 00
%n ->
/ %N -> N
%o -> o / %O -> O
%p -> AM / %P -> am
%q -> q / %Q -> Q
%r -> 12:00:00 AM / %R -> 00:00
%s -> 1208044800 / %S -> 00
%t -> / %T -> 00:00:00
%u -> 7 / %U -> 15
%v -> 13-Apr-2008 / %V -> 15
%w -> 0 / %W -> 14
%x -> 04/13/08 / %X -> 00:00:00
%y -> 08 / %Y -> 2008
%z -> +0000 / %Z -> Z
=> “a”..”z”

For those who need even more you can register a Proc which will be called with the Date/Time to format as an argument:

>> proc = lambda do |what|
?> what.day – 1
>> end

Don’t forget to register your custom formats with Date and Time if you deal with Date and Time formatting!

This entry was posted on Sunday, April 13th, 2008 at 13:40 and is filed under Ruby, Tech. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

back

2 Responses to “Custom Date/Time formats in Ruby (on Rails)”

  1. www.engel-bedeutung.de Says:
    November 30th, 2008 at 6:39

    Hi there I like your post “Custom Date/Time formats in Ruby (on Rails)” so well that I like to ask you whether I should translate into German and linking back. Greetings Engel

  2. godot Says:
    December 4th, 2008 at 11:18

    Hey Engel, sure, go right ahead. Post the link to it here as well, please.

Leave a Reply


simplificator is proudly powered by WordPress
Entries (RSS) and Comments (RSS).