为什么日期选择器中的日期显示最后一天? [英] Why is the date in datepicker angular showing the last day?

查看:123
本文介绍了为什么日期选择器中的日期显示最后一天?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用日期选择器c棱角材料.这是代码:

I use datepicker c angular material. Here's the code:

<td [formGroup]="item">
 <div class="input-group">
  <div class="input-group-addon">
   <mat-datepicker-toggle mdSuffix [for]="picker"></mat-datepicker-toggle>
   <mat-datepicker #picker></mat-datepicker>
  </div>
  <input class="form-control" [matDatepicker]="picker" placeholder="Date" formControlName="date">
 </div>
</td>

Datapicker的形式是在发送时将其发送到服务器.

Datapicker is in the form that sends it to the server when it is sent.

但是问题在于这些值是在最后一天传输的.

But the problem is that the values are transmitted over the last day.

例如,我选择了1/18/2018,但发送到服务器2018-01-17T22: 00: 00.000Z.

For example, I chose 1/18/2018, but sent to the server 2018-01-17T22: 00: 00.000Z.

奇怪的是,用于日期的角形管道正确地转换了日期,但是在显示日期之前,我在服务器上有一个请求,要求按月份进行分组,而新月份的第一天位于上一个日期的最后一天

It's strange that the angular pipe for date converts the date correctly, but before displaying it, I have a request on the server for grouping by the month and the first day of the new month falls on the last day of the previous one.

我存储在猫鼬模式中的日期类型为Date.

Date I store in mongoose schema with type Date.

也许有人对此有疑问.

谢谢.

推荐答案

我通过将datepicker日期值转换为UTC来解决此问题.这是示例:

I solve this problem by converting datepicker date value to UTC. Here is example:

const d = new Date(<DATE FROM FORM FIELD>);
const date = Date.UTC(d.getFullYear(), d.getMonth(), d.getDate());

也许不是修复它的好方法,但是可以帮助我

Maybe it's not elegant way to fix it but help me

这篇关于为什么日期选择器中的日期显示最后一天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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