Angular 中的当前日期减去 180 天

Subtract 180 days from current date in Angular

我正在为 Angular 日期选择器使用 KendoUI;定义今天的日期,我使用:

public value: Date = new Date();

如果我想把日期显示为今天减去180天怎么办?

这在 typescript/angular 4 等中。如在我的 component.ts 文件中:

import { Component, OnInit } from '@angular/core';

@Component({
   selector: 'app-record-filter-from-date',
   template: `
   <div class="form-group">
      <label>From Date:</label>
        <kendo-datepicker class="k-widget k-datepicker k-header crowd-control k-input" style="width: 100%;"
            [(ngModel)]="value"
            [format]="'dd-MMM-yyyy'"
            #dateModel="ngModel"
        ></kendo-datepicker>
</div>
`,
 styleUrls: ['./record-filter-from-date.component.css']
})

export class RecordFilterFromDateComponent implements OnInit {
  public value: Date = new Date();

  constructor() {

 }

 ngOnInit() {

 }
}
let today = new Date();
let day = this.today.getDate() - 180;
console.log(day);

JavaScript class Date 可能是自 1970 年 1 月 1 日以来经过毫秒构建的 00:00:00 UTC,因此您可以像这样使用它:

let date180DaysAgo = new Date(new Date().getTime() - 180 * 24 * 60 * 60 * 1000);
console.log(date180DaysAgo);

构造函数中的180是要减去的天数,180 * 24 * 60 * 60 * 1000给出180天内的毫秒数

使用Javascript的日期
var date180 = new Date(date.getTime()); date180.setDate(date.getDate() - 180);

非常感谢大家的回复,但是,对于 Angular,KendoUI 没有达到预期的效果。当前未记录的在 Kendo datePicker 对象中操作日期的方法是:

您需要 'addDays' @progress 模块:

import { addDays } from '@progress/kendo-date-math';

然后可以计算出他们期望的日期,在这种情况下:

public value: Date = addDays(new Date(), -180);

笨蛋:http://plnkr.co/edit/OIiCPfqnu4Nb4knZiPF1?p=preview