Time Management Frontend vs Backend

Screen Shot 2016-07-25 at 2.47.07 PM

I was having the biggest problem dealing with time on my small weather app. I created the logic on my Rails backend to take the current time format that time object into a 12 hour format then return the formatted date string on the frontend of my app. This worked perfectly… until I deployed the application. I used the Heroku to deploy and host the app, but within the deployment I would gain 4 hours on my app giving my shiny cool new app the wrong time.

Screen Shot 2015-06-03 at 11.01.39 AM

As a junior dev, at the time, I knew this must have been a problem based on where the Heroku servers where located; However I had no idea how to handle this in Rails and I was too new to JavaScript handle it on the frontend. So… to solve this I monkey patched a solution, I subtracted the date by 4 hours  and called it a day. The problem was solved until daylight savings rang it’s ugly head. Once daylight savings happened I had the wrong hour now matter what the time zone the app was opened in.

I really did not understand how to solve this problem until I learned more about the magic of JavaScript on the frontend of an application. I learned that the time will always be right for the user’s location on the user’s browser so I just asked the browser for the time.

Screen Shot 2016-07-25 at 3.16.29 PM.png

by doing this I by pass the backend, server, and Heroku which messed up the time on the app.

Now Forcast gif shows the right time and all is right in the word… for now.

Advertisements