Formatting of negative durations #1699
-
Hello, I am trying to format a negative duration using Luxon. Specifically, I have a duration of -1.5 hours, and I would like to display it as -01:30. However, when I use the following code:
I get the output -01:-30 instead of the desired -01:30. Could someone please help me understand how to correctly format a negative duration in this case (assuming this is possible)? Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 11 replies
-
I have looked into this and agree that it is buggy behavior. I think we should patch this, but maybe that would be breaking for some people, so I'll leave that decision to leadership. I can't imagine how this behavior would be useful to anyone as-is. It is not that pretty, but for practical purposes, you can suppress the negative sign using Intl.NumberFormat options, namely
|
Beta Was this translation helpful? Give feedback.
-
This is not necessarily desired for all durations. For example, Duration.fromObject({hours: -1.5}).toFormat('hh:mm'); // -01:-30 as before
Duration.fromObject({hours: -1.5}).toFormat('hh:mm', { leadingSignOnly: true }); // -01:30 What do you think? |
Beta Was this translation helpful? Give feedback.
-
After further consideration I have decided not to enable this behavior by default. Duration.fromObject({ hours: -1.5 }).toFormat("hh:mm", { signMode: "negativeLargestOnly" }): // => -01:30 |
Beta Was this translation helpful? Give feedback.
After further consideration I have decided not to enable this behavior by default.
Instead the next version will have a parameter to
toFormat
to enable this new behavior: