Moment.js - tomorrow, today and yesterday

后端 未结 12 2075
悲哀的现实
悲哀的现实 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 01:10

    So this is what I ended up doing

    var dateText = moment(someDate).from(new Date());
    var startOfToday = moment().startOf('day');
    var startOfDate = moment(someDate).startOf('day');
    var daysDiff = startOfDate.diff(startOfToday, 'days');
    var days = {
      '0': 'today',
      '-1': 'yesterday',
      '1': 'tomorrow'
    };
    
    if (Math.abs(daysDiff) <= 1) {
      dateText = days[daysDiff];
    }
    
    0 讨论(0)
  • 2021-01-30 01:14

    Here is how I do that using moment:

      let today = moment().format('DD MMMM YYYY');
    
      let tomorrow = moment().add(1, 'days').format('DD MMMM YYYY').toString();
    
      let yesterday = moment().subtract(1, 'days').startOf('day').format('DD MMMM YYYY').toString();
    
    
    0 讨论(0)
  • 2021-01-30 01:16

    I have similar solution, but allows to use locales:

        let date = moment(someDate);
        if (moment().diff(date, 'days') >= 1) {
            return date.fromNow(); // '2 days ago' etc.
        }
        return date.calendar().split(' ')[0]; // 'Today', 'yesterday', 'tomorrow'
    
    0 讨论(0)
  • 2021-01-30 01:17

    You can use this:

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

    In Moment.js, the from() method has the daily precision you're looking for:

    var today = new Date();
    var tomorrow = new Date();
    var yesterday = new Date();
    tomorrow.setDate(today.getDate()+1);
    yesterday.setDate(today.getDate()-1);
    
    moment(today).from(moment(yesterday)); // "in a day"
    moment(today).from(moment(tomorrow)); // "a day ago" 
    
    moment(yesterday).from(moment(tomorrow)); // "2 days ago" 
    moment(tomorrow).from(moment(yesterday)); // "in 2 days"
    
    0 讨论(0)
  • 2021-01-30 01:21

    You can customize the way that both the .fromNow and the .calendar methods display dates using moment.updateLocale. The following code will change the way that .calendar displays as per the question:

    moment.updateLocale('en', {
        calendar : {
            lastDay : '[Yesterday]',
            sameDay : '[Today]',
            nextDay : '[Tomorrow]',
            lastWeek : '[Last] ffffdd',
            nextWeek : '[Next] ffffdd',
            sameElse : 'L'
        }
    });
    

    Based on the question, it seems like the .calendar method would be more appropriate -- .fromNow wants to have a past/present prefix/suffix, but if you'd like to find out more you can read the documentation at http://momentjs.com/docs/#/customization/relative-time/.

    To use this in only one place instead of overwriting the locales, pass a string of your choice as the first argument when you define the moment.updateLocale and then invoke the calendar method using that locale (eg. moment.updateLocale('yesterday-today').calendar( /* moment() or whatever */ ))

    EDIT: Moment ^2.12.0 now has the updateLocale method. updateLocale and locale appear to be functionally the same, and locale isn't yet deprecated, but updated the answer to use the newer method.

    0 讨论(0)
提交回复
热议问题