Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single letter fromNow() format #2781

Closed
DarrylD opened this issue Nov 29, 2015 · 25 comments
Closed

Single letter fromNow() format #2781

DarrylD opened this issue Nov 29, 2015 · 25 comments

Comments

@DarrylD
Copy link

DarrylD commented Nov 29, 2015

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?

@DarrylD DarrylD changed the title Single letter format Single letter fromNow() format Nov 29, 2015
@alburthoffman
Copy link
Contributor

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"?

@DarrylD
Copy link
Author

DarrylD commented Nov 30, 2015

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

@dlindahl
Copy link

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'
    }
  });

@mattjohnsonpint
Copy link
Contributor

Yes, locale setting is the recommended approach.

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

@rmisio
Copy link

rmisio commented Feb 21, 2017

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.

@TheBITLINK
Copy link

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.

@msavin
Copy link

msavin commented Mar 13, 2018

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

@doriandrn
Copy link

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

@ThomasLabstep
Copy link

+1 - Looking for this right now.

@sumeet
Copy link

sumeet commented May 6, 2018

👍

2 similar comments
@jesper-bylund
Copy link

👍

@bluerface
Copy link

+1

@msavin
Copy link

msavin commented Jul 23, 2018

+99999999999999

@viv-li
Copy link

viv-li commented Jul 31, 2018

+1 please

@gdtrice
Copy link

gdtrice commented Aug 28, 2018

+1

@msavin
Copy link

msavin commented Aug 28, 2018

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

@tominou
Copy link

tominou commented Aug 28, 2018

+1 we need it

@NodiraIbrogimova
Copy link

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

@bettysteger
Copy link

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'
    }
  });

@charles-allen
Copy link

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

@puntozap
Copy link

puntozap commented Aug 26, 2020

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

@tedspare
Copy link

tedspare commented Jul 9, 2021

For anyone looking to copy and paste Instagram-like, ultra-minimal timestamps, this worked for me.

moment.locale('en', {
  relativeTime: {
    future: 'in %s',
    past: '%s ago',
    s:  '1s',
    ss: '%ss',
    m:  '1m',
    mm: '%dm',
    h:  '1h',
    hh: '%dh',
    d:  '1d',
    dd: '%dd',
    M:  '1M',
    MM: '%dM',
    y:  '1Y',
    yy: '%dY'
  }
})

As for file structure (maybe not obvious to all), locale does not need to be called at the point of use but can be tucked away in a helpers.js file. Thanks!

@bsor-dev
Copy link

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'
    }
  });

How to do this only in specific date not the whole moment in the page?

@bhanu-urbanx
Copy link

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"?

Just now

@anirban-baisya
Copy link

anirban-baisya commented Mar 19, 2024

Create an function and export it & use it like below (ex. getTimeAgo(date) )

import moment from "moment";

export const getTimeAgo = (date) => {
    const duration = moment.duration(moment().diff(date));
    const hours = Math.floor(duration.asHours());
    const days = Math.floor(duration.asDays());
    const weeks = Math.floor(duration.asWeeks());
  
    if (weeks > 0) {
      return `${weeks}w`;
    } else if (days > 0) {
      return `${days}d`;
    } else {
      return `${hours}h`;
    }
  };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests