Javascript FullCalendar time zone support with Rails
It's a little known fact, but even though I head up this awesome team of Rails Developers, I get to still get my hands dirty in the code base of applications.
We use the excellent FullCalendar JQuery plugin in some of our and our clients applications, but there was a situation where Rails seemed to be ignoring the time zone setting that FullCalendar was sending as part of it's update method.
FullCalendar would send a string "2013-01-25 11:00:00 +1100"
and Rails would save it to the database as "2013-01-25 11:00:00"
which basically meant it was being saved with it's time zone information stripped.
This didn't make sense as the Rails app had the following in application_controller.rb
:
around_filter :set_time_zone def set_time_zone(&block) Time.use_zone(current_user.time_zone, &block) end
And in the config/application.rb
file:
config.time_zone = 'UTC' config.active_record.default_timezone = 'UTC'
The solution though turned out to be simple, change FullCalendar to post in an ISO string representing the time, instead of a normal Javascript string.
So, we have the following (edited) config for FullCalendar:
$('div#fullcalendar').fullCalendar({ eventSources: [{ url: '/my/calendar_events', ignoreTimezone: false }], eventResize: function(event,dayDelta,minuteDelta,revertFunc) { updateEvent(event); } });
Then the update action is:
function updateEvent(the_event) { $.ajax({ type: 'PUT', url: "/my/calendar_events/" + the_event.id, data: { calendar_event: { name: the_event.title, starts_at: the_event.start.toISOString(), ends_at: the_event.end.toISOString(), all_day: the_event.allDay } }, dataType: "json" }); };
Notice the call to toISOString()
in there? That formats the time as "2013-01-25T01:00:00.000Z"
which means it's always sending UTC time. Rails parses this correctly and saves it into the database correctly and voila, no more time zone issues with FullCalendar.
Latest Articles by Our Team
Our expert team of designers and developers love what the do and enjoy sharing their knowledge with the world.
-
Is it worth upgrading your Rails application?
-
The Axioms of Software Development - Part 4
-
No app left behind: Upgrade your application to Ruby 3.0 and s...
We Hire Only the Best
reinteractive is Australia’s largest dedicated Ruby on Rails development company. We don’t cut corners and we know what we are doing.
We are an organisation made up of amazing individuals and we take pride in our team. We are 100% remote work enabling us to choose the best talent no matter which part of the country they live in. reinteractive is dedicated to making it a great place for any developer to work.
Free Community Workshops
We created the Ruby on Rails InstallFest and Ruby on Rails Development Hub to help introduce new people to software development and to help existing developers hone their skills. These workshops provide invaluable mentorship to train developers, addressing key skills shortages in the industry. Software development is a great career choice for all ages and these events help you get started and skilled up.