Skip to content

How to fix the Next & Previous Links When Your Application Uses The American DateTime Format

Francisco Quintero edited this page Oct 13, 2024 · 2 revisions

I ran into a problem where the next and previous links only work correctly if I had the following in my date_time.rb initializer:

DateTime::DATE_FORMATS[:default] = "%d/%m/%Y"

Or

DateTime::DATE_FORMATS[:default] = "%m/%Y"

However, I need the default date and datetime formats to use the American date format throughout the application so I can't use the formats above.

To fix the problem, I forked the gem and changed the following in simple_calendar/lib/simple_calendar/calendar.rb

From:

def url_for_next_view
  view_context.url_for(@params.merge(start_date_param => date_range.last + 1.day))
end

def url_for_previous_view
  view_context.url_for(@params.merge(start_date_param => date_range.first - 1.day))
end

To:

def url_for_next_view
  view_context.url_for(@params.merge(start_date_param => (date_range.last + 1.day).strftime("%m/%Y")))
end

def url_for_previous_view
  view_context.url_for(@params.merge(start_date_param => (date_range.first - 1.day).strftime("%m/%Y")))
end

Maybe there is a better way to do this but nothing I tried directly on the links in the simple_calender views worked for me.