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"

通过以上步骤,可以

相关内容

热门资讯

于此同时!wepoker底牌透... 于此同时!wepoker底牌透视(透视)好像有辅助安装(有挂神器)-哔哩哔哩1、wepoker底牌透...
透视挂!wepoker到底有没... 您好,wepoker到底有没有透视这款游戏可以开挂的,确实是有挂的,需要了解加去威信【1367043...
透视真的!佛手大菠萝辅助(透视... 透视真的!佛手大菠萝辅助(透视)真是真的是有辅助app(有挂神器)-哔哩哔哩1、佛手大菠萝辅助破解器...
针对!菠萝辅助器免费版的功能介... 针对!菠萝辅助器免费版的功能介绍(透视)原来存在有辅助安装(真的有挂)-哔哩哔哩1、针对!菠萝辅助器...
透视智能ai!wepoker私... 透视智能ai!wepoker私局代打(透视)真是存在有辅助辅助器(有挂助手)-哔哩哔哩wepoker...
透视真的!wpk真吗(透视)都... 透视真的!wpk真吗(透视)都是真的有辅助辅助器(揭秘有挂)-哔哩哔哩1、wpk真吗透视辅助软件激活...
透视工具!wepoker私人局... 透视工具!wepoker私人局辅助(透视)一直真的是有辅助安装(有挂方针)-哔哩哔哩1、实时wepo...
现有关情况通报如下!哈糖大菠萝... 现有关情况通报如下!哈糖大菠萝软件下载(透视)原来是有辅助app(有挂分析)-哔哩哔哩所有人都在同一...
近日!wepoker游戏的安装... 近日!wepoker游戏的安装教程(透视)真是真的是有辅助神器(有挂功能)-哔哩哔哩1、wepoke...
透视有挂!we-poker靠谱... 透视有挂!we-poker靠谱吗(透视)竟然真的有辅助软件(存在有挂)-哔哩哔哩we-poker靠谱...