0

I'm using a KendoUI for Angular datepicker; to define todays date, I use:

public value: Date = new Date();

What do I do if I want to display the date as todays date, minus 180 days?

This is in typescript/angular 4, etc.. as in my component.ts file:

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() {

 }
}
crunch
  • 355
  • 4
  • 16
  • 2
    Possible duplicate of [Subtract days from a date in JavaScript](https://stackoverflow.com/questions/1296358/subtract-days-from-a-date-in-javascript) – mrogers Jul 20 '17 at 21:56

4 Answers4

1

JavaScript class Date may be constructed passing milliseconds since 1 January 1970 00:00:00 UTC, so you can use it like:

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

180 in the constructor is number of days to subtract, 180 * 24 * 60 * 60 * 1000 gives number of milliseconds within 180 days

Andriy
  • 14,781
  • 4
  • 46
  • 50
1

I very much appreciate the replies from everyone, however, they did not function as expected with KendoUI for Angular. The currently undocumented way to manipulate the date in the Kendo datePicker object is:

You'll require the 'addDays' @progress module:

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

THEN one can do the math to get the date they are expecting, in this case:

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

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

crunch
  • 355
  • 4
  • 16
0
let today = new Date();
let day = this.today.getDate() - 180;
console.log(day);
Vega
  • 27,856
  • 27
  • 95
  • 103
0

Use Javascript's Date
var date180 = new Date(date.getTime()); date180.setDate(date.getDate() - 180);

ParUpadh
  • 37
  • 1
  • 1
  • 8