question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Formatting of negative durations contains extra minus chars

See original GitHub issue

When toFormat("hh:mm") is used on a negative Duration, the resulting string can have extra minus characters.

Example:

const x = Duration.fromObject({ minutes: 60 });
const y = Duration.fromObject({ minutes: 150 });
const diff = x.minus(y);
diff.toFormat("hh:mm"); // Outputs "-1:-30" rather than the expected "-1:30"

Example is available here: https://codesandbox.io/s/43wry09vw4

Version 1.10.0

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:5
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
munierujpcommented, Sep 18, 2020

My solution:

function isNegativeDuration (duration) {
  return duration.valueOf() < 0
}
function formatDuration (duration, pattern) {
  if (isNegativeDuration(duration)) {
    return `-${duration.negate().toFormat(pattern)}`
  } else {
    return duration.toFormat(pattern)
  }
}
formatDuration(Duration.fromObject({ minutes: 1 }), 'h:mm')  // 0:01
formatDuration(Duration.fromObject({ minutes: -1 }), 'h:mm')  // -0:01
4reactions
icambroncommented, Jan 25, 2019

Yeah, the formatter is a little dumb about negatives. It’s saying “(-1)😦-30)” because it doesn’t know how to pull the negative out as “-(1:30)”

Read more comments on GitHub >

github_iconTop Results From Across the Web

Optional Format for Negative Numbers
The minus sign is not included in the output if the negativePattern contains characters not in the positivePattern. It is assumed that the...
Read more >
Review guidelines for customizing a number format
A number format can have up to four sections of code, separated by semicolons. These code sections define the format for positive numbers,...
Read more >
How to use the TEXT function
The first container in the formula above (bolded) lets you format positive numbers, the minus sign formats positive numbers as negative numbers.
Read more >
How to display / show negative time properly in Excel?
Here is an easy and quick way for you to display the negative time normally in Excel by changing the Excel's Default Date...
Read more >
Display Negative Sign Character on the Right - YouTube
This video shows how to add a negative sign character to the right of a negative number instead of having it on the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found