The issue of Rails time lagging by a day persists upon deployment on my server

When a user clicks on a day in my calendar, I pass a JavaScript time variable to my Rails controller using Ajax.

Everything works perfectly when I test it locally, but once deployed on the server, the date appears to be one day behind the actual day clicked.

Upon initial page load with the calendar, the correct day is displayed and I utilize: day = Time.now.to_date.to_s(:db)

When a user selects a day on the calendar, the following JavaScript statement is executed:

start_date: Math.floor(new Date(date).getTime()/1000)

This translates to:

1362549600

and is then passed through Ajax into this Rails controller method:

start_date = Time.at(params[:start_date].to_i).to_date.to_s(:db)
logger.info "start_date = #{start_date}"
logger.info "time = #{Time.at(params[:start_date].to_i).to_datetime.to_s(:db)}"

The output from the logger is:

start_date = 2013-03-05
time = 2013-03-05 22:00:00

I have specified my timezone in config/application.rb as:

config.time_zone = 'Central Time (US & Canada)'

Do you have any suggestions on resolving this issue? Am I passing the date incorrectly from JavaScript to Rails?

EDIT

In my JavaScript code snippet, the initial value of date is

Wed Feb 06 2013 00:00:00 GMT-0600 (CST)

Another point worth mentioning is that my server operates on PDT while I am located in the central time zone.

$ date
Tue Mar 19 16:51:52 PDT 2013

When running my application locally, the logger output shows:

start_date = 2013-03-06
time = 2013-03-06 00:00:00

This difference between yesterday and today leaves me unsure about what needs to be adjusted to correct this discrepancy.

Answer №1

In Ruby, the Time.at method utilizes the server system time, similar to how all Date and Time functions operate within the language.

While the config.time_zone setting is mainly used for database date conversions (reading or writing), you can find more information on this topic in this helpful answer.

If you run irb on the server and execute

Time.at(1362549600).utc_offset / 60 / 60
, you may notice that the timezone offset may not align precisely with your expectations, resulting in a discrepancy like the -2 hours mentioned.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What steps can be taken to restrict users to providing only one comment and rating for each item?

In the backend controller, I have the following code snippet: 'use strict'; var Comment = require('../../../models/comment'); module.exports = { description: 'Create a Comment', notes: 'Create a comment&apos ...

Unleashing the Power of Dynamic JSON Data Access

I am facing an issue with my React Child component. Here is the code snippet: const SingleProject =(props)=>{ let data = projectData.VARIABLE_FROM_PROPS.projectDetails; let asideData = projectData.VARIABLE_FROM_PROPS.projectSideBar; useEffe ...

Attempting to hash the password led to encountering an error

An issue was encountered: both data and salt arguments are required. This error occurred at line 137 in the bcrypt.js file within the node_modules directory. The code snippet below highlights where the problem is present: const router = require("express" ...

Errors may occur when attempting to auto-refresh a page with a PHP-generated image using Ajax

When I include the image somepage.php in my code, it displays correctly. However, if I use Ajax to refresh the div containing somepage.php, the text becomes distorted. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

Try repeating the Ajax request until it is successful

Is there a way to continuously repeat an Ajax request until it returns 1, and stop if it returns 0? while(1){ $.ajax({ type: "POST", url: '/admin/importdata/', data: $info, dataType: "json", success: function($result) { ...

Dealing with Laravel and AJAX - Issue with Loading DIV

I find myself in a perplexing situation. Despite not encountering any errors in my apache logs or browser (chrome), I am faced with an issue that has left me baffled. On a specific page (localhost/admin/networks), I can click on an item from a database-ge ...

Customize the appearance of radio buttons in HTML by removing the bullets

Is there a way for a specific form component to function as radio buttons, with only one option selectable at a time, without displaying the actual radio bullets? I am looking for alternative presentation methods like highlighting the selected option or ...

Execution of Ajax call fails to occur synchronously

I have created a unique weather website that utilizes the flickr API as well as the yahoo API to gather weather data. However, I am facing an issue where the ajax call from the yahoo API does not retrieve the necessary data in time for the content of the p ...

What impact does adding 'ng' in unit tests have on how promises are handled?

Here is an example of a service that utilizes $q.when to wrap a promise from a third-party library: // myService.js angular.module('myApp', []) .service('myService', function($q, $window) { var api = new $window.MyAPI(); this ...

How come the mongoose ref feature is not functioning properly for me when I attempt to reference objects from a different schema?

I'm having trouble accessing the attributes of a store's address, as I keep getting 'undefined'. It seems that the address is only an id, even though I set up a 'ref' in the address schema. What could be causing this issue? H ...

Verify if the date surpasses the current date and time of 17:30

Given a date and time parameters, I am interested in determining whether that date/time is greater than the current date at 17:30. I am hoping to achieve this using moment js. Do you think it's possible? This is what I have been attempting: let ref ...

A guide on updating URLs that are not enclosed within an anchor tag using JavaScript

In my current scenario, I am dealing with text that includes URL links presented in two different formats. www.stackoverflow.com <a href="http://www.stackoverflow.com">Stack over flow</a> My objective is to create a straightforward function ...

Error: Unable to find axios module

screenshot of browser developer tool - network I have embedded the script in my base.pug file script(scr='https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.0/axios.min.js') Upon loading the page and inspecting the browser developer tool > netw ...

Confused about having to use window.variableName in my code and not understanding the reason

Working on a web app with JS, Angular, and Meteor that integrates the Youtube API has been quite challenging. In one of my controllers, I initialized the youtube player object in the constructor following the necessary steps outlined by the Youtube API. Ho ...

Combining various postponed JavaScript file imports in the HTML header into a single group

I've been facing an issue with my code structure, particularly with the duplication of header script imports in multiple places. Every time I need to add a new script, I find myself manually inserting <script type="text/javascript" src=&q ...

Leverage JavaScript E4X to cleverly rename specific XML elements

Currently, I am using JavaScript to manipulate XML without involving the DOM in a browser context. I need assistance with creating an E4X expression that can rename a list of tags based on a given substring. The challenge is that I may not know the exact t ...

Suggestions for updating the 'begin' and 'finish' variables transmitted through ajax on fullcalendar?

Shown below is the URL to request JSON data via Ajax: '/php/get-events.php?start=2015-05-31&end=2015-06-07&_=1433154089490'. This query will fetch JSON data from 2015-05-31 to 2015-06-07. However, I am looking to retrieve data over a ...

What can I do to resolve a node server issue with the error message "npm ERR! code ELIFECYCLE npm ERR! errno 1"?

npm ERROR! code ELIFECYCLE npm ERROR! errno 1 npm ERROR! [email protected] start: node server npm ERROR! Exit status 1 npm ERROR! npm ERROR! Task failed at the [email protected] start script. npm ERROR! This may not necessarily be an ...

Straightforward JSON issue

I am new to JSON and I need to work with it now. I have tried several examples from the jQuery page, but they don't seem to be working for me. I have a *.php file that generates a string. From what I understand, this is how I pass JSON data from PHP ...

"Delivering dynamic HTML content using React/Redux variables in the production environment

I am currently in the process of developing a React web application, and I have encountered an issue where a variable intended to store a schedule is being filled with the HTML content of the page when I build and serve the application. Interestingly, the ...