Home > Blockchain >  Angular get dates from last 24 hours in format dd/MM/yyyy HH:mm
Angular get dates from last 24 hours in format dd/MM/yyyy HH:mm

Time:11-12

I am trying to build a method that sets two date variables on today's date and the date of yesterday by the same time.

I have tried creating the date with let fecha:Date=new Date(), and using a pipe as I have seen in other questions, but they are a bit old and their answers do not work in my case. How could I parse that date to the format I want? I do not know how to create a date for the day before neither. Thanks a lot.

CodePudding user response:

    // getting new date
    let today = new Date();

    // getting previous date
    let yesterday = new Date(today.getTime() - (24*60*60*1000));

    // formatting the date
    let latest_date = this.datePipe.transform(yesterday, 'dd-MM-yyyy');

I have created a stackblitz link for you.Click here

  • Related