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

[help] Formatting duration. #1662

Open
BMitrovic17 opened this issue Oct 11, 2021 · 1 comment
Open

[help] Formatting duration. #1662

BMitrovic17 opened this issue Oct 11, 2021 · 1 comment

Comments

@BMitrovic17
Copy link

Is there a way to discard portion of the duration while formatting ?

To explain better:
All the duration information is presented via API always in seconds.

var dayjs = require("dayjs");
var duration = require("dayjs/plugin/duration");
dayjs.extend(duration)

let durOne = '183';
let durTwo = '8';

console.log(dayjs.duration(durOne, 'seconds').format('m [Min] s [secs]'));
// "3 Min 3 secs"
console.log(dayjs.duration(durTwo, 'seconds').format('m [Min] s [secs]'));
// "0 Min 8 secs"

How can i format it in a way that it would discard m [Min] in second instance?
To elaborate, I want to dynamically show proper duration, regardless of what API sends.

I know i can break it down to objects and threat each and every one of it separately and then combine but was wondering if there's a built in method like we have for escaping characters.
Something like .format('?(m [Min]) s [secs]') where question mark would serve as a checker.

Thanks in advance and sorry if it's a silly question, but it's an only way to learn :D

@warri93
Copy link

warri93 commented May 13, 2022

@BMitrovic17 @iamkun ,

Did you find a solution for this?

We are struggling with it as well.

For example we want to display PT11856H8M34S as years, days, hours, minutes & seconds. Leaving out the months.

dayjs.duration('PT11856H8M34S').add(0,'s').format('Y[y] D[d] H[h] m[m]')
Output: 1y 9d 0h 8m

dayjs.duration('PT11856H8M34S').add(0,'s').format('Y[y] M[m] D[d] H[h] m[m]')
Output: 1y 4m 9d 0h 8m

You can clearly see that we lose 4 months of days... How can we prevent this? It should output something like 1y 129d 0h 8m

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

2 participants