How to get yesterday's date with Momentjs?

后端 未结 8 849
清酒与你
清酒与你 2021-01-30 08:08

So, my question is simple, how do I get yesterday\'s date with MomentJs ? In Javascript it is very simple, i.e.

today = new Date();
yesterday = new Date(today.se         


        
8条回答
  •  说谎
    说谎 (楼主)
    2021-01-30 08:30

    This worked for me:

    var yesterday = new Date(dateInput.getTime());
    yesterday.setDate(yesterday.getDate() - 1);
    console.log(yesterday);
    
    var tomorrow = new Date(dateInput.getTime());
    tomorrow.setDate(tomorrow.getDate() + 1);
    console.log(tomorrow);
    
    dateB = moment(yesterday).format("YYYYMMDD");
    dateA = moment(tomorrow).format("YYYYMMDD");
    console.log(dateB);
    console.log(dateA);
    

提交回复
热议问题