momentjsmomentics

How to get "Yesterday 17th May 2015" format in moment.js


I need to format some days in the format "Yesterday 17th May 2015" using moment.js. This is what I have so far:

var asd = moment(moment.utc("2015-09-30T08:35:40.59"))
          .subtract(1, 'days')
          .calendar();

Solution

  • As described in the Calendar customization doc is it possibile to customize the format of calendar.

    In the example below, I customized the sameElse format of calendar for the English locale to return a string with the format you required:

    moment.locale('en', {
        calendar : {
            sameElse : '[Yesterday] Do MMM YYYY'
        }
    });
    
    var asd = moment.utc("2015-09-30T08:35:40.59")
              .subtract(1, 'days')
              .calendar();
    
    console.log(asd);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.js"></script>