0

I got an input field where the user inputs a date, currently I'm using Chronic and the user likes it.

If the user enters last jan using Chronic and strftime I get January 2013 which is good, the issue comes when:

the user input is an upcoming month like January, the parsing I'm doing returns January 2014 and not January 2013 wich is what the user wants.

result_date = Chronic.parse(params[:date]).strftime('%B %Y')

Is there a way that I can set as default the current year? So when the user puts January it is parsed to January 2013 ?

Thanks!

M.Octavio
  • 1,780
  • 2
  • 25
  • 39

1 Answers1

0

I was able to figure it out!

result_date = Chronic.parse(params[:date], :context => :past).strftime('%B %Y')

Now if the user input is a single month like Jan the outcome will be January 2013

I hope it helps someone else!

M.Octavio
  • 1,780
  • 2
  • 25
  • 39
  • 1
    Did you test it with a future month within the same year? For example, you're asking this question in June 2013. If you parse "July" with `:context => :past` you should get 2012, not 2013. – jdl Jun 13 '13 at 17:39
  • yes, in my case works fine, this date is for pulling data from DB so no future data is available. when the user types July it refers to previous month or year in this case. – M.Octavio Jun 13 '13 at 19:59