Moment.js - tomorrow, today and yesterday

后端 未结 12 2299
悲哀的现实
悲哀的现实 2021-01-30 00:21

I\'d like the moment().fromNow() functionality, but when the date is close it is too precise - ex. I don\'t want it to show \'in 3 hours\' but \'today\' - so basica

12条回答
  •  野性不改
    2021-01-30 00:56

    From 2.10.5 moment supports specifying calendar output formats per invocation For a more detailed documentation check Moment - Calendar.

    **Moment 2.10.5**
    moment().calendar(null, {
      sameDay: '[Today]',
      nextDay: '[Tomorrow]',
      nextWeek: 'ffffdd',
      lastDay: '[Yesterday]',
      lastWeek: '[Last] ffffdd',
      sameElse: 'DD/MM/YYYY'
    });
    

    From 2.14.0 calendar can also take a callback to return values.

    **Moment 2.14.0**
        moment().calendar(null, {
         sameDay: function (now) {
           if (this.isBefore(now)) {
             return '[Will Happen Today]';
           } else {
            return '[Happened Today]';
           }
           /* ... */
          }
        });
    

提交回复
热议问题