javascript - 如何在 Angular 6 中设置响应式(Reactive)表单的日期默认值?

标签 javascript angular timestamp angular-forms

我有日期过滤器的表单,我尝试设置日期输入的开始日期和结束日期的默认值。

<form [formGroup]="filter" (ngSubmit)="applyFilter()">
<mat-form-field>
    <input matInput [matDatepicker]="start" formControlName="start" placeholder="Начальная дата">
    <mat-datepicker-toggle matSuffix [for]="start"></mat-datepicker-toggle>
    <mat-datepicker #start></mat-datepicker>
</mat-form-field>
<mat-form-field>
    <input matInput [matDatepicker]="end" formControlName="end" placeholder="Конечная дата">
    <mat-datepicker-toggle matSuffix [for]="end"></mat-datepicker-toggle>
    <mat-datepicker #end></mat-datepicker>
</mat-form-field>

以及TS部分

refreshFilter() {
  const now = new Date();
  const monthAgo = new Date().setMonth(now.getMonth() - 1).toString();
  console.log(monthAgo)
  console.log(now)
  this.filter = new FormGroup({
  start: new FormControl(monthAgo, []),
  end: new FormControl(now, [])
  });

}

一个月前的 console.log()1533908066234,但新日期是 2018 年 9 月 10 日星期一 16:34:26 GMT+0300 和带有时间戳的表单输入不起作用。如何获取一个月前的正确格式日期以成功设置到 FormControl 中?

最佳答案

如果你想在 Angular 中格式化日期,你可以使用 DatePipe 尝试使用管道进行格式化。 这允许您根据区域设置规则格式化日期值。 如果您需要更多相关信息,您还可以在此处查看: https://angular.io/api/common/DatePipe

本月也执行Ago.toLocaleDateString()

希望这会有所帮助!

关于javascript - 如何在 Angular 6 中设置响应式(Reactive)表单的日期默认值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52259142/

相关文章:

javascript - 使用 Jest 进行测试 - TypeError : cannot read property 'forEach' of undefined

javascript - React axios 多文件上传

Angular : Unable to GET local json using HttpClient

angular - Jenkins - Angular : how to execute ng build after git push

python - 将毫秒设置为小时,以 pandas 为单位,最多 24 小时

java - 数据库中的时间戳与文本文件中的时间戳格式不同

javascript - 使用 DWR 的示例程序

javascript - 如何在 javascript 中显示图像二进制字符串?

angular - Angular 2.0 中的变更检测

mysql - TimeStamp 字段和 mysql curdate 之间有什么区别