减去Moment和LocalDate之间的转换天 [英] Subtract day in conversion between Moment and LocalDate

查看:101
本文介绍了减去Moment和LocalDate之间的转换天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么,当您输入日期时,它会给我回原日期但又少了一天的时间.

service.ts

 受保护的convertDateFromClient(项目:IProject):IProject {const复制:IProject = Object.assign({},project,{entryDate:project.entryDate!= null吗?moment(project.entryDate,'DD/MM/YYYY').format('YYYY-MM-DD'):null});返回副本;} 

Project.java

  ...@Column(名称="entry_date")私有LocalDate entryDate;公共LocalDate getEntryDate(){返回entryDate;}公共项目entryDate(LocalDate entryDate){this.entryDate = entryDate;返回这个;}公共无效setEntryDate(LocalDate entryDate){this.entryDate = entryDate;} 

输入日期:2020年12月2日,返回日期:2020年1月1日

更新:如果我通过键盘在datePicker的输入中输入日期为12/02/2020(dd/MM/yyyy),我将从POST 01/012020(MM/dd/yyyy)中获取

任何建议?

解决方案

您可能会遇到时区问题,请记住,在JavaScript中,Date类表示时间戳,是自1970年1月1日以来的毫秒数(00:00)UTC.

Mozilla Javascript文档-日期

请看一下这篇帖子,进行有趣的讨论((Javascript日期对象是否总是一天休假?)[

The date entered: 12/2/2020 and return: 11/2/2020

Update: If I put the date 12/02/2020 (dd/MM/yyyy) by keyboard in the datePicker´s input, i recived from POST this 01/12/2020 (MM/dd/yyyy)

Any suggestions???

解决方案

You might be having a problem with time zones, remember that in JavaScript Date class represent a timestamp, is the number of miliseconds since January 1, 1970 00:00 UTC.

Mozilla Javascript Doc - Date

Take a look at this post for an interesting discussion, (Is the Javascript date object always one day off?)[Is the Javascript date object always one day off?

I have been using one of this methods to solve this issue,

export function dateFromModel(d: Date): Date {
  const doo = new Date(d);
  return new Date(doo.getTime() + Math.abs(doo.getTimezoneOffset() * 60000));
}

UPDATE: I think you now have the problem I mention, plus you now are switching month and days when using moment. Just print the date before using moment and after, to check this. You can also use toISOString() to check what are you going to receive in the backend, this JavaScript function complies to ISO 8601 just like Java LocalDate.

这篇关于减去Moment和LocalDate之间的转换天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆