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

add moment().intervalNow() for bidding, etc... #740

Closed
crucifyer opened this issue Apr 23, 2013 · 3 comments
Closed

add moment().intervalNow() for bidding, etc... #740

crucifyer opened this issue Apr 23, 2013 · 3 comments

Comments

@crucifyer
Copy link

moment("2013-04-27T05:48:29.000Z").intervalNow()
"3 days 15 hours 2 minutes 53 seconds"

moment("2013-04-27T05:48:29.000Z").intervalNow(1)
"in 3 days 15 hours 2 minutes 53 seconds"

need for exact left time without timezone

thank you.

--- moment.orig.js  2013-04-23 23:38:38.945626866 +0900
+++ moment.js   2013-04-23 23:34:49.304488112 +0900
@@ -436,6 +436,7 @@
             future : "in %s",
             past : "%s ago",
             s : "a few seconds",
+            ss : "%d seconds",
             m : "a minute",
             mm : "%d minutes",
             h : "an hour",
@@ -1163,6 +1164,25 @@
             return this.from(moment(), withoutSuffix);
         },

+       interval : function (time, withoutSuffix) {
+           var i = this.diff(time),
+               d = moment.duration(Math.abs(i)),
+               a = d._data,
+               l = d.lang(),
+               f = [];
+           if(a.years) f.push(l.relativeTime(a.years, 0, 'yy'));
+           if(a.months) f.push(l.relativeTime(a.months, 0, 'MM'));
+           if(a.days) f.push(l.relativeTime(a.days, 0, 'dd'));
+           if(a.hours) f.push(l.relativeTime(a.hours, 0, 'hh'));
+           if(a.minutes) f.push(l.relativeTime(a.minutes, 0, 'mm'));
+           if(a.seconds) f.push(l.relativeTime(a.seconds, 0, 'ss'));
+           return withoutSuffix ? l.pastFuture(i, f.join(' ')) : f.join(' ');
+       },
+
+       intervalNow : function (withoutSuffix) {
+           return this.interval(moment(), withoutSuffix);
+       },
+
         calendar : function () {
             var diff = this.diff(moment().startOf('day'), 'days', true),
                 format = diff < -6 ? 'sameElse' :
@ichernev
Copy link
Contributor

That looks interesting. Can you form it as a pull request? I guess it would also need tests, and adding ss to all languages :)

@timrwood
Copy link
Member

This is a pretty sweet addition, but I think it will have the same issues as outlined in this comment when localizing the string concatenation.

Also, this seems like a better addition to the Duration.prototype than the Moment.prototype.

@timrwood
Copy link
Member

Closing in favor of #745

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

3 participants