jsmreese / moment-duration-format Goto Github PK
View Code? Open in Web Editor NEWFormat function plugin for the Moment Duration object.
License: MIT License
Format function plugin for the Moment Duration object.
License: MIT License
Im trying to convert this:
86532 minutes
but getting:
(1M 4w 2d 2h:12m)
im using format("y[y] M[M] w[w] d[d] h[h]:mm[m]")
Why is it not rendering 2M 0w ?
moment.duration(29, "days").format()
"29d" => correct
moment.duration(30, "days").format()
"0m" => Should be "30d"
moment.duration(60, "days").format()
"1m" => Should be "60d"
moment.duration(90, "days").format()
"2m" => Should be "90d"
I ๐ the dependence of underscorejs and lo-dash but if it really needs, put the dependency in package json.
I am trying to format ms into a "progress" time.
so what i would like is for
moment.duration(120000).format(_theFormat); // "2:00"
moment.duration(31000).format(_theFormat); // "0:31"
moment.duration(3620000).format(_theFormat); // "1:00:20"
Is there already a way to do that?
(i dont think so)
On way would be to use format h:m:ss
but with an "option" to trim h but not mm.
I hope it make sense
I am getting the error with Moment 2.9.0 and 2.10.0 when loading this library via browserify:
Uncaught TypeError: Cannot set property 'format' of undefined
From: https://github.com/jsmreese/moment-duration-format/blob/master/lib/moment-duration-format.js#L202
Thoughts?
Thanks for the work on this, it works wonderfully on the server side!
Can I use the Moment locales with this plugin? And if so, how?
I'd like to have a format like: 4 hours 58 minutes.
I tried
moment.duration(17880, 'seconds').format("hh mm");
but that leaves me with 04 58
Edit:
I've got it working now by using this:
moment.duration(17880, 'seconds').format("h ["+ moment._locale._relativeTime.hh.slice(3) +"] m ["+ moment._locale._relativeTime.mm.slice(3) +"]");
if there is native support via this duration plugin please let me know.
Formatting a duration of whole minutes results in only the minutes as digits.
So a duration of say 600 seconds will be formatted as "10" and a duration is 10 seconds is also formatted at "10".
I would have expected the 600s to show as "10:00" or perhaps "10m" - and the 10s as "00:10" or "10s"
@jsmreese apologies if i'm missing something here but is there anyway currently to output hh:mm:ss
where all digits are displayed all of the time?
I found no way to display 1 hour
and 2 hours
. I can show either of those: 1 hours, 2 hours
or 1 hour, 2 hour
.
Is there a way or is this feature missing?
After including moment-duration-format.js I get following error on Chrome (Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36) when rendering the calendar.
Uncaught TypeError: Cannot read property 'hasTime' of undefined
Fullcalendar depends on moment.js but I did not dig deeper to find the root cause of the problem.
I'm using the plugin like this:
var test = moment.duration(3562296); // milliseconds
test.format("HH:mm:ss");
The result is:
"59:22"
The result should be:
"59:22:00"
Hi, there seams to be a calculation error somewhere:
moment.duration(1000_60_26, 'milliseconds').humanize() --> '26 Minutes'
moment.duration(1000_60_26, 'milliseconds').format('m') --> 25
Please i need some assistance using this library with
http://ionden.com/a/plugins/ion.rangeSlider/demo_advanced.html
There are no JavaScript errors ... the slider simply stops working once i apply the duration format
Please help,
Thanks.
<script src="js/jquery-2.1.1.js"></script>
<script src="js/plugins/ionRangeSlider/ion.rangeSlider.min.js"></script>
<script src="js/plugins/moment/moment.min.js"></script>
<script src="js/plugins/moment/moment-duration-format.js"></script>
<script type="text/javascript">
$(function () {
$("#txtDuration").ionRangeSlider({
min: +moment.duration(15, "minutes").format("h [hrs], m [min]"),
max: +moment.duration(300, "minutes").format("h [hrs], m [min]"),
type: 'single',
step: 5,
prettify: function (num) { return moment.duration(num, "minutes").format("h [hrs], m [min]"); }
});
$("#txtNegative").ionRangeSlider({
min: 0,
max: 3,
type: 'single',
step: 0.1,
prettify: false
});
});
</script>
To reproduce:
var dur = moment.duration(0, "minutes");
dur.format("m", 10);
// "0.undefined0"
Getting errors with webpack when using this module...
WARNING in ..//moment-duration-format/lib/moment-duration-format.js/moment-duration-format/lib/moment-duration-format.js 189:17-34
Module not found: Error: Cannot resolve module 'moment' in /home/ubuntu/workspace/node_modules/moment-duration-format/lib
@ ../
moment.duration( -42, "seconds" ).format( "h:m:s.S" )
-> "42.0"
moment.duration( -42, "hours" ).format( "h:m:s.S" )
-> "-42:0:0.0"
moment.duration( -42, "seconds" ).format( "h:m:s.S", 0, { trim: false })
-> "-0:0:42.0"
Looks like a negative sign is trimming with leading tokens.
I calculate diff in a loop for an unknown number of times and get the seconds back
It may be 0 or more.
When 0 it shows
00
when minutes come in
01:01
when hours come in
01:01:01
I am using format
moment.duration(_diff, "seconds").format("hh:mm:ss", { forceLength: true });
I want it to show this if zero seconds
00:00:00
I want it to show if minutes
00:02:23
The plugin is really great but why on earth would you want to rewrite the formatting functions, in a strange formatting "template" syntax?
If I specify hh:mm:ss
I want it to show 00:00:00
as in .NET/ Java / Python even moment does it.
If I wanted only seconds to show using that format, then I would check if the seconds are <60 and format it using ss
, yea?
Is there any work-around for this, use moment formatter instead?
I am extremely picky on how the text is padded and displayed.
Great job on the moment.duration plugin +1 not so great trying to rewrite date formatting :(
moment.duration(1800, 'seconds').format('hh:mm')
returns 30 but I expect 00:30. Is there any way to do it?
moment.duration(2, 'days').format('d[ days] h[ hours]');
// 2 days 0 hours
I'd like to be able to drop the 0 hours part
I tried {trim: 'right'}
:
moment.duration(2, 'days').format({
template: 'd[ days] h[ hours]',
trim: 'right',
});
// 2 days 0
I expected it to produce 2 days
not 2 days 0
.
It's either a bug or {trim: 'right'}
is not meant for what I tried to use it (although the name makes me think it is)
This issue does not apepar to work any longer - you can not use this with moment-timezone - you had a bug where you were specifically requiring moment which i changed but still it wont work ---
const momentDurationFormat = require('moment-duration-format')
== undefined
I'm desperately trying to get the following format: mm:ss:SS
while SS
is double digit milliseconds.
This is the code i use:
var formattedTime = moment.duration(ms).format('mm:ss:SS', {
trim: false,
precision: 0
});
this is before I start counting up the ms
variable (as in, starting the timer):
It's correct.
This is after I start the timer:
This is not correct.
I add 10 to ms
every 10 milliseconds via an interval.
Any ideas what I might be doing wrong?
It'd be great if this plugin supported input from the html5 duration format.
PT23H 9M 2.343S
= 23 hours, 9 minutes and 2.345 seconds
Would this be a feature worth having? I'm pulling my data from html and would love to have the html match the spec (e.g. <time datetime="PT@(seconds)S" class="js-momentDuration"></time>
). If so I can work on a pull request.
See:
http://www.brucelawson.co.uk/2012/best-of-time/
http://www.w3.org/TR/html-markup/time.html
I am writing a small reformatter, that discards all token except the two leftmost meaningful ones, i.e.:
moment.duration({'minutes': -10}).format('h[H] m[M]')
"10M"
While "-10M" would be expected, like:
moment.duration({'minutes': -10}).format('m[M]')
"-10M"
While positive values are trimmed correctly:
moment.duration({'minutes': 10}).format('h[H] m[M]')
"10M"
Same applies when a "bigger" token is present in format string while the passed duration is more than the negative "bigger" token. This is illustrated by:
moment.duration({'hours': -1, 'minutes': -10}).format('h[H] m[M]')
"-1H 10M"
moment.duration({'hours': -1, 'minutes': -10}).format('d[D] h[H] m[M]')
"1H 10M"
Or should such cases be handled manually in my use case?
Hi
It's possible that I missed something in the documentation but this is what I have
moment.duration(356, 'minutes').format('mm');
356 minutes is 5h56m. The format results in 356
instead of 56
. If I have the following code:
moment.duration(356, 'minutes').format('h:mm');
This will result in 5:56
. But I don't need the hour part, only the minutes part. How can I solve this?
This may be a trivial issue, but it's giving me a hard time... How would I go about using moment-duration-format in my Angular controllers?
I've imported momentJS and MDF JS file in my index.html:
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.min.js"></script> <script src="bower_components/moment-duration-format/lib/moment-duration-format.js"></script>But when I call the below line in a controller, I get an error:
console.log( moment.duration(123, "minutes").format("h:mm") );
Error:
TypeError: moment.duration(...).format is not a function...
To support RequireJS, you're doing this:
if (typeof require === "function") {
try { moment = require('moment'); }
catch (e) { }
}
And that won't work. It's not the correct way to support RequireJS. It spews out this error:
Error: Module name "moment" has not been loaded yet for context: _. Use require([])
And I'm not even using underscore nor lodash. That's strange, but, the problem is the require()
call. Calling require like this is not how you call RequireJS, according to the documentation:
http://requirejs.org/docs/errors.html#notloaded
Ultimately, the result of this problem is:
uncaught exception: Moment Duration Format cannot find Moment.js
Because for RequireJS's gas factory reasons, Moment hadn't been loaded at that time.
TL;DR
Either use the define
function and put require()
inside it, or use the async require([ ])
version.
moment.duration(2 * (moment.duration(1, 'months').valueOf())).format('M d') resolves to:
1 30 00:00:00.000 instead of 2 0 00:00:00.000.
moment.duration(3 * (moment.duration(1, 'months').valueOf())).format('M d') resolves to:
2 29 00:00:00.000 instead of 3 0 00:00:00.000.
Months need to be calculated in the same way that they are in Moment for consistency.
moment.duration(13, 'minutes').format("HH:mm [uur]", { trim: false });
Will return "00:12 uur"
I would expected "00:13 uur"
Thanks!
Function should be called with the first argument as the duration object itself so that template strings may be dynamically generated.
When you output only the higher unit types (like years or hours, depending on the data you've got) it's usually expected that you round the value not always down, but sometimes up. For example
moment.duration(123, "minutes").format("h [h]");
must result into "2 h"
anyway, but
moment.duration(179, "minutes").format("h [h]");
is usually expected (in real life) to result into "3 h"
, not "2 h"
.
Of course, this option should be optional, like:
moment.duration(179, "minutes").format("h [h]");
// "2h"
moment.duration(179, "minutes").format("h [h]", { allowRoundUp: true });
// "3h"
There are methods that are either different or not in underscore that moment-duration-format uses, so underscore does not work.
I recommend that you just take the statement that underscore is an option out of the documentation.
Specifically, _.rest and _.initial do not support callbacks in underscore like they do in LoDash. Also _.isPlainObject does not exist in underscore.
Let's say I have the following:
moment.duration(121, "minutes").format("h [hrs], m [min]");
which outputs: 2 hrs, 1 min
but If I have
moment.duration(90, "minutes").format("h [hrs], m [min]");
then I get: 1 hrs, 30 min
Ideally, I'd get: 1 hr, 30 mins instead of 1 hrs, 30 min
How can I account for singular/plural forms of "hr/hrs" and "min/mins"?
Is there a way to regex or otherwise format to accomplish this?
Could this plugin support humanize()
?
It would be awesome if we could use momentjs i18n support: http://momentjs.com/docs/#/i18n/
Or is it up to us to provide a template that uses i18n?
moment.duration(600, 'seconds').format()
-> "10"
moment.duration(601, 'seconds').format()
-> "10:01"
moment.duration(602, 'seconds').format()
-> "10:02"
moment.duration(603, 'seconds').format()
-> "10:02" #<- WAT
moment.duration(604, 'seconds').format()
-> "10:04"
Am I doing something wrong?
I use moment-timezone and would also like to use moment-duration-format - but moment-timezone loads its own copy of moment, so the copy you patch for moment-duration-format is not the same one that has the moment-timezone added.
I have been able to work around the issue with the code below - but a more direct approach would be nice...any chance you could export an initializer to which I could pass the moment instance we want the plugin applied to?
require("moment-duration-format");
var momentTimezone = require("moment-timezone");
momentTimezone.duration = require("moment").duration;
I want to trim a date from both sides, so that a duration that can range from a minute to a couple of months won't show
"0 months 0 days 1 hour 0 minutes 0 seconds" but instead just shows "1 hour"
Would be nice if there was a short instruction how to install this plugin. I really have no idea how to bring this to life :(
Nodejs 0.10.25
console.log( "MOMENTS: " + moment.duration( 3, 'minutes' ) + " " + moment.duration( 3, 'minutes' ).format( 'hh:mm' ) + " " + moment.duration( 3, 'minutes' ).format( 'hh:mm:ss' ) + " " + moment.duration( 3, 'minutes' ).format( ) )
prints
MOMENTS: 180000 03 03:00 3
This doesn't look like what is being explained in the README (and is not what I want either).
What's wrong?
What is format to add AM/PM ?
Consider the following case:
moment.duration.fn.format.defaults.trim = false;
moment.duration('0:15:45').format('hh:mm'); // "00:15"
The format function is truncating the duration value rather than rounding it. In the case above, it would be more accurate to round it first, and display "00:16". Is there a setting to do that?
Would love to be able to trim on a different moment, the reason for this is i want to show a progress ie
0:10/2:30 however due to trimming its displayed as 10/2:30
I could turn trimming off completely but then it would be 00:00:10/00:02:30 because I still want the hours for those durations that are this long.
moment.duration(24, 'hours').format('d[d] h[h]')
"1d 0h"
But from the docs, I thought the 0h would be omitted by default:
expected: "1d"
is this intended, and is there any extra options to change it?
thanks!
While the most recent commit is viable to use as a bower component, there is no bower.json
file included in this repo, nor has there been a release since v1.0.0 which required Underscore.String as a dependency.
Would you say that the plugin is ready for a patch release, maybe 1.0.1, with the inclusion of a bower.json file? I'm happy to fork & submit a PR.
3:43.977 with a precision of 2 is formatted incorrectly as 3:43.97. It should be 3:43.98. Can't use a pattern to hide/show values. Have to implement rounding.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.