The database has unfortunately registered an incorrect value for the "date" input after it was saved

When I select a date from the datepicker input field, it is being incorrectly saved in the database.

I am selecting the date using the datepicker and then using AngularJS to send it to Spring MVC

AngularJS:

$scope.updateProjectDetails = function(detail) {
    $http.post('${pageContext.request.contextPath}/api/details', detail)
    .then(function(response) {
        console.log(response)
    });
}

Chrome Console:

config: {method: "POST", transformRequest: Array(1), transformResponse: Array(1), paramSerializer: ƒ, url: "/editor-application/api/details", …}
data:
date: 1557439200000
hours: 2
id: 76
projectId: 53

1557439200000 -> 5/10/2019, 12:00:00 AM

Then the JSON is posted to the MVC mechanism:

Controller:

@PostMapping(path = "/details")
public ProjectDetails updateProjectDetails(@RequestBody ProjectDetails details) {

    details.setId(0);
    editorService.updateProjectDetails(details);
    return details;
}  

DAO:

@Override
@Transactional
public void updateProjectDetails(ProjectDetails details) {

    Session currentSession = sessionFactory.getCurrentSession();
    currentSession.saveOrUpdate(details);
}

And in the database:

76 2019-05-09 2 53

The date always appears to be one day behind. I understand there may be a timezone issue, but how can I resolve it?

Answer №1

I have discovered that the code snippet below works well:

// fetching data from the server
$http.get('myDate').then(date => // date === 1557439200000
    new Date(date-(new Date(date).getTimezoneOffset()*60*1000)).toISOString().slice(0,10)
)

While it may require a bit of boilerplate code, this solution effectively accomplishes the task at hand.

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

Display an input field in VueJS with a default value set

Dealing with a form containing various editable fields, I devised a solution. By incorporating a button, clicking it would conceal the label and button itself, while revealing a text box alongside a save button. The challenge lays in pre-filling the textbo ...

Tips on calculating the combined value of price and quantity simultaneously

Greetings, kindly bear with me as my knowledge of JS scripting is quite limited. My expertise lies more in PHP programming. I stumbled upon this neat and straightforward script that calculates the total of product table rows and also provides the grand t ...

Tips for managing an outdated AngularJS 1.5.5 application

Currently tasked with maintaining an outdated AngularJS 1.5.5 application, I am unsure how to properly set up the environment for it. What specific versions of NodeJS/NPM do I need to install in order for the gulp tasks to function correctly? Is there an ...

Tips for troubleshooting aspx pages following an ajax post back?

I am currently working on a project with a unique architecture. During the page_load event, we utilize a switch case structure: switch (command) { case "LOAD": load(); break; case "UNLOAD": ...

JavaScript's counter variable can become confusing when used in an if statement

Struggling as a beginner in javascript, I find myself stuck on writing an If statement that triggers after the fourth turn. My goal is to have an alert pop up once the user has clicked on four options. The counter variable "turns" was added to track prog ...

Oops! There was an issue with the form field - make sure to include a MatFormFieldControl for proper validation on the

I am working on an Angular application that utilizes Angular Material components. Despite conducting extensive research online, I have not been able to find a suitable solution to the specific error message I have encountered. The issue revolves around a ...

Leveraging Angular variables within attributes that are not native to Angular

One thing that puzzles me about Angular is the inconsistency in using braces. For example, when I try to pass an Angular variable into an ng-click function: ng-click="getComments('{{post.Id}}')" It may look fine in the HTML code, but when I ac ...

Is there a way for me to determine the version of Angular that I am currently using?

Is there a way for me to figure out which version of Angular I am currently using? I attempted the following: angular --version angular --v angular -version angular -v Unfortunately, I received this error: -bash: angular: command not found Based on yeo ...

How does Facebook access the new hashtags when they are clicked on?

I've been developing a website recently and I'm really impressed with the way a popup page appears when you click on a hashtag. I've been wanting to incorporate a similar feature for a while now, but I'm not sure how to do it. Could thi ...

Having difficulty executing the playwright tests

Trying to execute the playwright test from the specified location results in a message prompting to run npm install -D @playwright/test before running the test. Despite having already installed playwright as a dev dependency, the test is still not being ex ...

Unable to pass data to the onChange event for the material-ui datePicker components

Need help with a form that includes a material-ui DatePicker. Here is an example: <DatePicker name="startDate" autoOk={true} floatingLabelText="startDate" onChange={(x, event) => {console.log(arguments);}} /> When I change the date, the console ...

The custom button feature is unresponsive to the enter key

Within my Angular project, I have implemented a custom button component that serves as a submit-form-button across various sections of the application. The issue arises when attempting to submit any form on the website using the enter key while focused on ...

Difficulties with choosing predecessors

Is there a way in the HTML snippet below to extract the checkall from the thing? .. <input type="checkbox" class="checkall"><label>Check all</label> <ul> <li><input type="checkbox" class="thing"><label>Thing 1 ...

Finding a date from a calendar with a readonly property in Playwright

Just starting out with the playwright framework after working with Protractor before. I'm trying to figure out the correct method for selecting a date in Playwright. selector.selectDate(date) //having trouble with this ...

Why does my value always revert to 0 whenever I switch screens while utilizing async storage?

Utilizing a stack Navigator, my primary screen is tracker.js, and the secondary one is macros.js. In macros.js, I have the ability to manually input nutritional macros (calories, Fats, Carbs, Protein) and add them to my UsedDailyCalories. However, when I ...

definition of a function with another function

I'm curious about whether it's considered a good practice in JavaScript to define a function within another function. Take a look at this code snippet: module.exports = function() { function foo() { // do something } ... foo() .. ...

Encountering an issue while trying to initiate a fresh React Native Project

As I work through the setup steps outlined in the React Native Documentation on my M1 MacBook Pro, I encounter a stumbling block. Despite successfully running React projects and Expo projects on this machine before, I hit a snag when trying to create a new ...

How to use AJAX script to call a non-static page method in ASP.NET

Is it possible to achieve this task without utilizing the UpdatePanel feature? ...

Arrange the array based on the key values

I am currently working with a function that sorts by name and an array containing value/key pairs. I am trying to figure out how I can dynamically pass the key on which the sort is being performed, so that I can call the same function each time with diffe ...

The div is incorrect and causing the label and input to move in the wrong direction

Whenever I try to adjust the position of the current-grade-name-input, the entire grade-point-input moves along with it inexplicably. /* final grade calculator: (wanted-grade - (current-grade * (1 - final%))) / final% Ex: have a 80 an ...