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

fromNow short hand #3049

Closed
MartyBolton opened this issue Mar 17, 2016 · 7 comments
Closed

fromNow short hand #3049

MartyBolton opened this issue Mar 17, 2016 · 7 comments

Comments

@MartyBolton
Copy link

Is there a really simple suggestion where I can override all the relative past times to short hand? I would think this is really useful for mobile. I don't need a string a mile long for a mobile screen.

Hoping to change these to short hand, like 1m or 1d or 1w - I think some other small time social apps do this already. I think someone already posted about similar thing n secongs ago should be just now. This is a similar request.

How can I override this format to short hand using the component, or do I do my own string replace?
thanks

  _relativeTime : {
            future : 'in %s',
            past : '%s ago',
            s : 'a few seconds',
            m : 'a minute',
            mm : '%d minutes',
            h : 'an hour',
            hh : '%d hours',
            d : 'a day',
            dd : '%d days',
            M : 'a month',
            MM : '%d months',
            y : 'a year',
            yy : '%d years'
        },

using 

angular.module('app')
  .filter('fromNow', function() {
    return function(date) {
      return moment(date).fromNow();
    }
  });
@maggiepint
Copy link
Member

Assuming you are running 2.12.0, the following would do the trick:

moment.updateLocale('en', {
    relativeTime : {
        future: "in %s",
        past:   "%s ",
        s:  "s",
        m:  "m",
        mm: "%d m",
        h:  "h",
        hh: "%d h",
        d:  "d",
        dd: "%d d",
        M:  "a mth",
        MM: "%d mths",
        y:  "y",
        yy: "%d y"
    }
});

moment().subtract(2, 'minutes').fromNow()
"2 m "

If you are running pre 2.12.0, it would be:

moment.locale('en', {
    relativeTime : {
        future: "in %s",
        past:   "%s ",
        s:  "s",
        m:  "m",
        mm: "%d m",
        h:  "h",
        hh: "%d h",
        d:  "d",
        dd: "%d d",
        M:  "a mth",
        MM: "%d mths",
        y:  "y",
        yy: "%d y"
    }
});

You could replace my hastily done shorthand with whatever you wanted.

It would also be an option to create a new locale, something like "en-mobile", that inherited from the parent locale. Then when you were on desktop you could use long form, and when you were on mobile you could flip over.
The parentLocale feature is a 2.12.0 feature.

moment.locale('en-mobile', {
parentLocale:'en',
    relativeTime : {
        future: "in %s",
        past:   "%s ",
        s:  "ssssssssssss",
        m:  "m",
        mm: "%d m",
        h:  "h",
        hh: "%d h",
        d:  "d",
        dd: "%d d",
        M:  "a mth",
        MM: "%d mths",
        y:  "y",
        yy: "%d y"
    }
});

Is that what you needed?

@MartyBolton
Copy link
Author

Hi Maggie - thanks! I'll try that! Thanks for the code example too and fast reply!
Marty

@maggiepint
Copy link
Member

Closing since I think the problem is solved.

@rmisio
Copy link

rmisio commented Feb 21, 2017

(I also posted this comment on #2781 - sorry for the spam)

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.

@mikerockett
Copy link

+1

Additionally, there are often times when you need long-hand and short-hand in different parts of the app… In those instances, updating locales seems a bit overkill… Or maybe that’s just me?

@ZainaliSyed
Copy link


moment.updateLocale('en', {
    relativeTime : {
        future: "in %s",
        past:   "%s ",
        s:  "s",
        m:  "m",
        mm: "%d m",
        h:  "h",
        hh: "%d h",
        d:  "d",
        dd: "%d d",
        M:  "a mth",
        MM: "%d mths",
        y:  "y",
        yy: "%d y"
    }
});

REFERENCE

IMPLEMENTATION GUIDANCE

MomentJS change 'a day ago' to '1d'

@kalnode
Copy link

kalnode commented Nov 9, 2022

Any movement on this?

I agree the strings are way too long for mobile.

I'm shying away from using the suggested locale trick, as ultimately I need everything: short form, long form AND multiple locales.

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

6 participants