首页 > 解决方案 > 如何在 nz datepicker angular 中设置 CURRENT DATE 和 HOUR 的值

问题描述

这是代码:https ://stackblitz.com/edit/angular-8beznk?file=src/app/app.component.ts

HTML

 <nz-range-picker class="pr-2" [nzDisabledDate]="disabledDate" [nzFormat]="dateFormat" [(ngModel)]="dateRange" (ngModelChange)="onChange($event)"></nz-range-picker>
     <nz-radio-group [(ngModel)]="radioValue" (ngModelChange)="onChangeTime($event)">
              <label nz-radio nzValue="3">Last 3 hours</label>
              <label nz-radio nzValue="12">Last 12 hours</label>
              <label nz-radio nzValue="240">Last 10 days</label>
              <label nz-radio nzValue="744">Last 31 days</label>
              <label nz-radio nzValue="2232">Last 3 months</label>
            </nz-radio-group>

TS

dateFormat = 'MM-dd-yyyy'
  dateRange: any;
  dateFilter: any;

  disabledDate = (current: Date): boolean => {
    return differenceInCalendarDays(current, new Date()) > 0;
  };

  onChange(date: any) {
    const DATE_FROM = format(new Date(date[0]), 'YYYY-MM-DD');
    const DATE_TO = format(new Date(date[1]), 'YYYY-MM-DD');

    const FILTER = { 'from': DATE_FROM, 'to': DATE_TO };

    console.log(FILTER);
  }

  onChangeTime(hour: any) {
    const CURR_DATE = format(new Date(), 'MM-DD-YYYY');
    const HOUR = format(subHours(CURR_DATE, hour), 'MM-DD-YYYY');

    const FILTER = { 'hour': hour };

    console.log(FILTER);
    console.log('FROM: ' + CURR_DATE + '| TO: ' + HOUR)
  }

我在这里尝试做的是,当我从收音机选择中单击时,它应该设置CURR_DATE从 START 和HOUR从 END。

例如,如何在 datepicker 中设置一个值。当前日期是 06-08-2020,它应该显示在 datepicker 中,START然后从收音机选择中选择过去 3 个月,然后是 03-07-2020,然后它应该显示在END.

标签: javascriptangulartypescriptng-zorro-antd

解决方案


推荐阅读