AngularMaterial日期选择器中日期不正确-日期为确切日期前一天。
创始人
2024-10-28 10:01:29
0

出现这种问题的原因可能是时区设置错误或者时间戳未被正确处理。为了解决这个问题,可以尝试以下几个步骤:

  1. 创建一个新的Date对象时传入所需的时区,例如:

const currentDate = new Date().toLocaleString("en-US", { timeZone: "Asia/Taipei" });

  1. 在组件中添加代码以将时间转换为时间戳:

const timestamp = new Date(currentDate).getTime();

  1. 在使用MatDatepicker模块时,使用ValueAccessor的提供程序来确保输入的正确性:

import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms'; import { MatDatepickerInputEvent } from '@angular/material';

export const VALUE_ACCESSOR: any = { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => DatepickerValueAccessor), multi: true };

@Component({ selector: 'datepicker-value-accessor', providers: [VALUE_ACCESSOR], template: }) export class DatepickerValueAccessor implements ControlValueAccessor{

// Initialization of variables date: any; onTouched: () => void; onChange: (_: any) => void;

// Writes a new value to the element writeValue(value: any) { this.date = value; }

// Registers a callback function that should be called when the control's value changes in the UI registerOnChange(fn: (_: any) => void): void { this.onChange = fn; }

// Fires when the control is blurred registerOnTouched(fn: () => void): void { this.onTouched = fn; } }

  1. 最后,如果还存在问题,请确定日期格式正确。尝试使用以下选项之一将日期的格式化更改为所需的格式:

dateFormat="yyyy-MM-dd" dateFormat="MM/dd/yyyy" dateFormat="dd/MM/yyyy"

通过以上步骤,可以

相关内容

热门资讯

热点推荐!hhpkoer辅助器... 热点推荐!hhpkoer辅助器视频,wepoker破解版内购,必胜教程(有挂神器);1.hhpkoe...
三分钟了解!wejoker辅助... 1、三分钟了解!wejoker辅助脚本,wpk作弊最怕三个东西,新2025教程(有挂攻略)(UU p...
盘点十款!wpk俱乐部辅助器,... 盘点十款!wpk俱乐部辅助器,wepoker免费透视,攻略教程(有挂透视);1分钟了解详细教程(微信...
免费测试版!约局吧德州有挂吗,... 免费测试版!约局吧德州有挂吗,拱趴大菠萝辅助神器,详细教程(有挂神器);小薇(透视辅助)致您一封信;...
玩家攻略推荐!poker wo... 玩家攻略推荐!poker world辅助,hhpkoer辅助挂是真的吗,我来教教你(有挂技巧);1分...
最新研发!哈糖大菠萝怎么开挂,... 最新研发!哈糖大菠萝怎么开挂,哈糖大菠萝挂,教你攻略(有挂辅助);致您一封信;亲爱哈糖大菠萝怎么开挂...
查到实测辅助!wepoker辅... 查到实测辅助!wepoker辅助器官方,wepoker线上大神,系统教程(有挂神器)这是由厦门游乐互...
总算清楚!pokermaste... 1、总算清楚!pokermaster修改器,hhpoker德州透视,力荐教程(有挂方法)。2、pok...
玩家必备攻略!wepoker有... 玩家必备攻略!wepoker有辅助插件吗,xpoker怎么作弊,透明教程(有挂攻略)是由北京得wep...
揭秘关于!wpk插件,we p... 揭秘关于!wpk插件,we poker插件,扑克教程(有挂透明)是一款可以让一直输的玩家,快速成为一...