How to get the previous date in angular?

后端 未结 3 1950
广开言路
广开言路 2021-01-12 05:49

Please help me getting the previous dates in angular 4.

currentdate: Date;
this.currentdate = new Date();
console.log(this.datePipe.transform(this.currentdat         


        
相关标签:
3条回答
  • 2021-01-12 06:20

    I am not sure if the year -1 thing is always valid since February 29th issue. I think more accurate way is that

    let aDate = new Date();
    aDate.setDate(aDate.getDate() - numberOfDaysBefore);
    

    replace 365 with number of days you want. Thank you!

    0 讨论(0)
  • 2021-01-12 06:26

    Try this

    let dte = new Date();
    dte.setDate(dte.getDate() - 2);
    console.log(dte.toString());

    0 讨论(0)
  • 2021-01-12 06:38

    The accepted answer is correct for javascript, this works in Angular:

    export class MyClass implements OnInit {
      // This gives you todays date.
      private dateToday: Date = new Date();
      // This also gives you todays date if you don't alter it on init.
      private dateYesterday: Date = new Date();
    
      constructor() { }
    
      ngOnInit() {
        // Alters the date
        this.dateYesterday = new Date(this.dateYesterday.setDate(this.dateYesterday.getDate() - 1));
      }
    
    }
    
    
    0 讨论(0)
提交回复
热议问题