rubydatechronic

does chronic have any options of date format it parses? (ruby)


I need to tell chronic that the format of date is day-month-year is that possible? The data I pass to chronic could also be words today/yesterday/2 days ago.

Currently chronic gives me 2 Dec 2010 instead of 12 Feb 2010 from 12-02-2010

The only solution I can think of is to swap day and month before passing the string to chronic.

require 'chronic'   

puts "12-02-2010 = #{Chronic.parse('12-02-2010')}"  #should be 12 Feb 2010


puts "yesteday = #{Chronic.parse('yesterday')}" #working ok
puts "Today = #{Chronic.parse('today')}"        #working ok

Solution

  • I've found this question today, 20 months after it has been asked. It seems that there is a way to indicate to swap months and days. Just use the :endian_precedence option:

    :endian_precedence (Array) — default: [:middle, :little] — By default, Chronic will parse "03/04/2011" as the fourth day of the third month. Alternatively you can tell Chronic to parse this as the third day of the fourth month by altering the :endian_precedence to [:little, :middle]

    Example here:

    Chronic.parse('12-02-2010').strftime('%d %b %Y')   #=> 02 Dec 2010 
    Chronic.parse('12-02-2010', :endian_precedence => [:little, :median]).strftime('%d %b %Y') #=> 12 Feb 2010
    

    Hope this helps!

    Dorian