Git Product home page Git Product logo

moment-duration-format's People

Contributors

hyuraku avatar rob--w avatar thib3113 avatar ticky avatar twipped avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

moment-duration-format's Issues

Is there a setting to round value before formatting it?

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?

Using RequireJS causes error

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.

"-" sign trimmed if minutes is negative but more than -60 and hours token is in format

I am writing a small reformatter, that discards all token except the two leftmost meaningful ones, i.e.:

  • 1 year 1 month 1 day => '1y 1m'
  • 1 month 1 day 1 hour => '1m 1d'
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?

Whole minutes and seconds display ambiguously

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"

Missing installation instruction

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 :(

Issue when using with fullcalendar.io

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.

'1d 0h' being displayed

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!

Strange conversion

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 ?

Allow coordinated trimming of multiple durations with the same settings

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.

Months are not calculated as they are in Moment

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.

Incorrect output for negative durations

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.

calculation error

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

Support rounding up

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"

Assistance using this library with ion.rangeSlider

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>

Rounding Milliseconds Incorrectly

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.

Updating release for use as a bower component

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.

Using moment-duration-format with Angular

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...

Padding/Leading Zeros doesn't work?

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"

choose which token to trim/show when value is 0

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

AM PM

What is format to add AM/PM ?

Only show the minute part 'mm' results in total number of minutes

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?

Formatting issues

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 :(

How to account for singular/plural formatting?

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?

Incorrect output

 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?

Problem using 1.3 together with moment 2.10.2

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?

Moment-Timezone and this is Incompatible (again)

#23

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

i18n usages?

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.

Over 30 days does not work

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"

Trim both

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"

underscore cannot be used as a replacement for LoDash

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.

Using with moment-timezone

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;

Trim: 'right' works incorrectly

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)

unit name pluralization

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?

milliseconds are always three digit

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):
image
It's correct.

This is after I start the timer:
image
This is not correct.

I add 10 to ms every 10 milliseconds via an interval.

Any ideas what I might be doing wrong?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.