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
Comments
I don't think moment has this feature right now. Actually you can write ur own function to do this, so not sure if moment really needs to support it. |
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:
|
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. |
👍 |
2 similar comments
👍 |
+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;
}; |
+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:
|
+1 having a 2nd function; I want both short+long forms available. |
my solution for to add a format for example one day or one year or something format, I did like this:
return for example 1d, 3y, 1m Regards |
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), |
How to do this only in specific date not the whole moment in the page? |
Just now |
Create an function and export it & use it like below (ex. getTimeAgo(date) )
|
Attempting to use one letter to describe format with
moment().fromNow();
Example: opposed to
1 hour
,1 day
,1 week
. Looking to have1h
,1d
,1w
.Is it a setting I'm missing to use a single letter format like so?
The text was updated successfully, but these errors were encountered: