简体   繁体   English

保存后,输入=“日期”在数据库中的无效值

[英]input=“date” invalid value in database after save

After picking date from input type="date", date-picker, it is wrongly stored into database. 从输入类型为“ date”的日期选择器中选择日期后,将其错误地存储到数据库中。

I'm picking the date from datepicker and then using AngularJS sending it to Spring MVC 我从datepicker中选择日期,然后使用AngularJS将其发送到Spring MVC

angularJS: angularJS:

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

chrome console: chrome控制台:

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 1557439200000-> 5/10/2019,12:00:00 AM

Then JSON is posted to MVC mechanism: 然后将JSON发布到MVC机制:

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 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天,我知道存在时区问题,但是该如何解决呢?

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. 这有点样板,但可以完成工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM