【问题标题】:Subtract 180 days from current date in Angular从 Angular 中的当前日期减去 180 天
【发布时间】:2017-07-20 21:54:03
【问题描述】:

我正在为 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() {

 }
}

【问题讨论】:

标签: angular kendo-ui kendo-ui-angular2


【解决方案1】:

JavaScript 类 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 天内的毫秒数

【讨论】:

    【解决方案2】:

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

    您需要“addDays”@progress 模块:

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

    那么可以通过数学计算得到他们期望的日期,在这种情况下:

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

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

    【讨论】:

      【解决方案3】:
      let today = new Date();
      let day = this.today.getDate() - 180;
      console.log(day);
      

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 2012-04-29
          • 1970-01-01
          • 2017-04-28
          • 1970-01-01
          • 2020-05-06
          • 2013-01-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多