After picking date from input type="date", date-picker, it is wrongly stored into database.

I'm picking the date from datepicker and then using AngularJS sending 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 JSON is posted to 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 database:

76 2019-05-09 2 53

Date is always -1 day, I know there is an issue of timezone, but how should I address it?

1 Answers

1
4lackof On Best Solutions

I've always found the following works:

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

It's a bit of boilerplate, but gets the job done.