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"

通过以上步骤,可以

相关内容

热门资讯

据权威媒体报道!黑侠破解wep... 据权威媒体报道!黑侠破解wepoker,微乐小程序辅助开发,揭露脚本(有挂神器)-哔哩哔哩1、在黑侠...
脚本教你!we poker游戏... 脚本教你!we poker游戏下,wepoker辅助器安装包(透视)其实有方法(哔哩哔哩)1、有没有...
透视有挂!德普之星辅助器怎么用... 透视有挂!德普之星辅助器怎么用,微乐自建房脚本最新版本更新内容,详细透视(有挂细节)-哔哩哔哩1、这...
脚本曝光!wepoker提高好... 脚本曝光!wepoker提高好牌率,wepoker可以开透视吗(透视)其实真的是有攻略(哔哩哔哩)小...
于此同时!wepoker插件程... 于此同时!wepoker插件程序激活码,微乐自建房安装挂免费,解谜透视(有挂教程)-哔哩哔哩1、这是...
脚本有挂!aapoker插件下... 脚本有挂!aapoker插件下载,aapoker俱乐部靠谱吗(透视)本来真的有攻略(哔哩哔哩)脚本有...
刚刚!newpoker怎么安装... 刚刚!newpoker怎么安装脚本,微乐小程序免费脚本,了解脚本(有挂教学)-哔哩哔哩1、玩家可以在...
透视揭露!wpk官网下载链接,... 透视揭露!wpk官网下载链接,wpk真的有透视嘛(透视)其实真的是有器(哔哩哔哩)1、完成有辅助插件...
透视模拟器!hhpoker是内... 透视模拟器!hhpoker是内部控制吗,微信小程序微乐破解器2024,解迷脚本(有挂秘诀)-哔哩哔哩...
透视专业!wpk模拟器多开,w... 透视专业!wpk模拟器多开,wpk插件(透视)好像存在有挂(哔哩哔哩)1、免费辅助多个强度级别选择2...