Moment.js - tomorrow, today and yesterday

后端 未结 12 2090
悲哀的现实
悲哀的现实 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:54
    const date = moment(YOUR_DATE)
    return (moment().diff(date, 'days') >= 2) ? date.fromNow() : date.calendar().split(' ')[0]
    
    0 讨论(0)
  • 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]';
           }
           /* ... */
          }
        });
    
    0 讨论(0)
  • Requirements:

    • When the date is further away, use the standard moment().fromNow() functionality.
    • When the date is closer, show "today", "yesterday", "tomorrow", etc.

    Solution:

    // call this function, passing-in your date
    function dateToFromNowDaily( myDate ) {
    
        // get from-now for this date
        var fromNow = moment( myDate ).fromNow();
    
        // ensure the date is displayed with today and yesterday
        return moment( myDate ).calendar( null, {
            // when the date is closer, specify custom values
            lastWeek: '[Last] ffffdd',
            lastDay:  '[Yesterday]',
            sameDay:  '[Today]',
            nextDay:  '[Tomorrow]',
            nextWeek: 'ffffdd',
            // when the date is further away, use from-now functionality             
            sameElse: function () {
                return "[" + fromNow + "]";
            }
        });
    }
    

    NB: From version 2.14.0, the formats argument to the calendar function can be a callback, see http://momentjs.com/docs/#/displaying/calendar-time/.

    0 讨论(0)
  • 2021-01-30 01:02

    I use a combination of add() and endOf() with moment

    //...
    const today = moment().endOf('day')
    const tomorrow = moment().add(1, 'day').endOf('day')
    
    if (date < today) return 'today'
    if (date < tomorrow) return 'tomorrow'
    return 'later'
    //...
    
    0 讨论(0)
  • 2021-01-30 01:03

    You can also do this to get the date for today and tomorrow and yesterday

    let today     = moment();
    
    let tomorrow  = moment().add(1,'days');
    
    let yesterday = moment().add(-1, 'days');
    
    0 讨论(0)
  • 2021-01-30 01:09

    You can use .add() and .subtract() method to get yesterday and tomorrow date. Then use format method to get only date .format("D/M/Y"), D stand for Day, M for Month, Y for Year. Check in Moment Docs

     let currentMilli = Date.now()
     let today = Moment(currentMilli).format("D/M/Y");
     let tomorrow = Moment(currentMilli).add(1, 'days').format("D/M/Y");
     let yesterday = Moment(currentMilli).subtract(1, 'days').format("D/M/Y");
    

    Result will be:

    Current Milli - 1576693800000
    today - 19/12/2019
    tomorrow - 18/12/2019
    yesterday - 18/12/2019
    
    0 讨论(0)
提交回复
热议问题