Skip to content
Branch: master
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

date-formats - read / parse and print dates (and times) from around the world


Did you know? Ruby's built-in standard date parser (that is, Date.parse) only supports English month and day names. Example:

Date.parse( '1 Jan' )
#=> <Date: 2019-01-01 ((2458485j,0s,0n),+0s,2299161j)>

Let's try Spanish:

Date.parse( '1 Ene' )
#=> invalid date (ArgumentError) in `parse`

What to do? Let's welcome the date-formats library that lets you parse dates in foreign languages. Example:

require `date/formats`
DateFormats.parse( '1 Ene', lang: 'es' )
#=> <Date: 2019-01-01 ((2458485j,0s,0n),+0s,2299161j)>

Or in French:

DateFormats.parse( 'Lundi 1 Janvier', lang: 'fr')
#=> <Date: 2019-01-01 ((2458485j,0s,0n),+0s,2299161j)>

How does it work? The date-formats library uses text patterns (that is, regular expressions) for defining and parsing new date formats. See the formats.rb script for all current supported formats and languages.

For example, the Spanish date 1 Ene gets matched by:


Where MONTH_ES is any of:

Enero      Ene
Febrero    Feb
Marzo      Mar
Abril      Abr
Mayo       May
Junio      Jun
Julio      Jul
Agosto     Ago
Septiembre  Sept  Sep  Set
Octubre    Oct
Noviembre  Nov
Diciembre  Dic

And the French date Lundi 1 Janvier gets matched by:

   \.?        # note: make dot optional

Where DAY_FR is any of:

Lundi     Lun  L
Mardi     Mar  Ma
Mercredi  Mer  Me
Jeudi     Jeu  J
Vendredi  Ven  V
Samedi    Sam  S
Dimanche  Dim  D

And MONTH_FR is any of:

Janvier    Janv   Jan
Février    Févr   Fév
Mars              Mar
Avril      Avri   Avr
Juillet    Juil
Septembre  Sept
Octobre    Octo   Oct
Novembre   Nove   Nov
Décembre   Déce   Déc

And so on.


The date-formats scripts are dedicated to the public domain. Use it as you please with no restrictions whatsoever.

Questions? Comments?

Send them along to the Open Sports & Friends Forum/Mailing List. Thanks!

You can’t perform that action at this time.