Moment: Single letter fromNow() format

Created on 29 Nov 2015  ·  21Comments  ·  Source: moment/moment

Attempting to use one letter to describe format with moment().fromNow();

Example: opposed to 1 hour, 1 day, 1 week. Looking to have 1h, 1d, 1w.

Is it a setting I'm missing to use a single letter format like so?

Most helpful comment

You can set this via locale:

  moment.locale('en', {
    relativeTime: {
      future: 'in %s',
      past: '%s ago',
      s:  'seconds',
      ss: '%ss',
      m:  'a minute',
      mm: '%dm',
      h:  'an hour',
      hh: '%dh',
      d:  'a day',
      dd: '%dd',
      M:  'a month',
      MM: '%dM',
      y:  'a year',
      yy: '%dY'
    }
  });

All 21 comments

I don't think moment has this feature right now.
You can refer to http://momentjs.com/docs/#/displaying/fromnow/

Actually you can write ur own function to do this, so not sure if moment really needs to support it.
One interesting question is if moment supports this feature, then what's the short label for "a few seconds"?

It would be an "s" (example: 5s, 35s) but, have the amount of seconds. Dealing with small amount of realestate on a project.

You can set this via locale:

  moment.locale('en', {
    relativeTime: {
      future: 'in %s',
      past: '%s ago',
      s:  'seconds',
      ss: '%ss',
      m:  'a minute',
      mm: '%dm',
      h:  'an hour',
      hh: '%dh',
      d:  'a day',
      dd: '%dd',
      M:  'a month',
      MM: '%dM',
      y:  'a year',
      yy: '%dY'
    }
  });

Yes, locale setting is the recommended approach.

See: http://momentjs.com/docs/#/customization/relative-time/

I would really love to see this feature be implemented in the core codebase. These instagram style short abbreviations are pretty ubiquitous these days.

Yes, I agree, it is quite easy to customize for one language. But... if your app needs to support multiple locales, suddenly you need to handle customizing it in the all langs you support. It would be really nice to have the power of moment's community take that burden on.

Not only apps that need to support multiple locales, but apps that need to display both the short and the long format in different parts would also benefit from this. Right now, i have to use a workaround in mine.

+1 - this kind of what thing is what made moment so great

+1 (& bump), would also love to see this happening

+1 - Looking for this right now.

:+1:

👍

+1

+99999999999999

+1 please

+1

var timeSince = function(date) {
  if (typeof date !== 'object') {
    date = new Date(date);
  }

  var seconds = Math.floor((new Date() - date) / 1000);
  var intervalType;

  var interval = Math.floor(seconds / 31536000);
  if (interval >= 1) {
    intervalType = 'y';
  } else {
    interval = Math.floor(seconds / 2592000);
    if (interval >= 1) {
      intervalType = 'm';
    } else {
      interval = Math.floor(seconds / 86400);
      if (interval >= 1) {
        intervalType = 'd';
      } else {
        interval = Math.floor(seconds / 3600);
        if (interval >= 1) {
          intervalType = "h";
        } else {
          interval = Math.floor(seconds / 60);
          if (interval >= 1) {
            intervalType = "m";
          } else {
            interval = seconds;
            intervalType = "now";
          }
        }
      }
    }
  }

  if (interval > 1 || interval === 0) {
    intervalType += 's';
  }

  return interval + ' ' + intervalType;
};

Source: https://stackoverflow.com/questions/3177836/how-to-format-time-since-xxx-e-g-4-minutes-ago-similar-to-stack-exchange-site

+1 we need it

@dlindahl Thank you so much for awesome solution. Now I know how to handle local changes for date formats

I did this for English and German:

  moment.updateLocale('en', {
    relativeTime: {
      future : 'in %s',
      past   : '%s ago',
      s  : function (number, withoutSuffix) {
        return withoutSuffix ? 'now' : 'a few seconds';
      },
      m  : '1m',
      mm : '%dm',
      h  : '1h',
      hh : '%dh',
      d  : '1d',
      dd : '%dd',
      M  : '1mth',
      MM : '%dmth',
      y  : '1y',
      yy : '%dy'
    }
  });
  moment.updateLocale('de', {
    relativeTime: {
      future : 'in %s',
      past : 'vor %s',
      s  : function (number, withoutSuffix) {
        return withoutSuffix ? 'jetzt' : 'ein paar Sekunden';
      },
      m  : '1min',
      mm : '%dmin',
      h  : '1Std',
      hh : '%dStd',
      d  : '1T',
      dd : '%dT',
      M  : '1M',
      MM : '%dM',
      y  : '1J',
      yy : '%dJ'
    }
  });

+1 having a 2nd function; I want both short+long forms available.
I'd suggest 'mo' for month(s) in English

my solution for to add a format for example one day or one year or something format, I did like this:

let time=moment(re.date).fromNow(true);
let format=this.formatDate(time);
formatDate(date){
    let strDate=date.split(" ");
    let format="";
    if(strDate[0]=="un"||strDate[0]=="a"){
      strDate[0]="1";
      format=strDate[0]+strDate[1][0];
      return format
    }
    format=strDate[0]+strDate[1][0];
    return format

  }

return for example 1d, 3y, 1m

Regards

Was this page helpful?
0 / 5 - 0 ratings

Related issues

IbraheemAlSaady picture IbraheemAlSaady  ·  3Comments

alvarotrigo picture alvarotrigo  ·  3Comments

dogukankotan picture dogukankotan  ·  3Comments

BCup picture BCup  ·  3Comments

benhathaway picture benhathaway  ·  3Comments