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

How we can format the duration in dayjs. #965

Closed
iamrahman opened this issue Jul 22, 2020 · 20 comments · Fixed by #1202
Closed

How we can format the duration in dayjs. #965

iamrahman opened this issue Jul 22, 2020 · 20 comments · Fixed by #1202
Labels
enhancement New feature or request released

Comments

@iamrahman
Copy link

iamrahman commented Jul 22, 2020

I have tried o use this but it does not work

import dayjs from 'dayjs';
import duration from 'dayjs/plugin/duration';
dayjs.extend(duration);
const toTimeFormat = (milliseconds: number) => {
  return dayjs.duration(milliseconds).format('HH:mm:ss')
};
@iamrahman iamrahman changed the title How we can formate the duration in dayjs. How we can format the duration in dayjs. Jul 22, 2020
@iamkun iamkun added the enhancement New feature or request label Jul 22, 2020
@iamkun
Copy link
Owner

iamkun commented Jul 22, 2020

Will add this later.

@iamrahman
Copy link
Author

@iamkun , Please let me know once this issue is fixed

@Prozhou
Copy link

Prozhou commented Sep 7, 2020

@iamkun let me know too.

@EarlOld
Copy link

EarlOld commented Sep 18, 2020

We need it too.

@js1m
Copy link

js1m commented Nov 9, 2020

Yes, please. I don't wanna do maths.

@francoism90
Copy link

Would like this feature as well, at the moment the following provides some workarounds: https://stackoverflow.com/questions/1322732/convert-seconds-to-hh-mm-ss-with-javascript

@verzac
Copy link
Contributor

verzac commented Nov 10, 2020

Hey gang (and @iamkun - need your input on this),

I'm happy to work on a PR for this, but would like some clarification on the expected behaviour of the API: this format won't exactly work like a normal .format(), would it?

const duration = dayjs.duration(1, 'hours').add(15, 'minutes')

// example A, it calculates mm (minutes) for that duration including the 1 hour in that duration
duration.format('mm') === '75'

// example B, it works like a normal format, but is probably less useful for working with "durations"
duration.format('mm') === '15'

In addition, do we want the API to do a mix of both A and B, depending on formatStr? i.e.:

// example C - a bit of a pain to implement and probably a bit confusing, but could potentially be useful for working with durations?
duration.format('mm') === '75'
duration.format('HH:mm') === '01:15'

Also, unlike the normal format, I'd probably ignore:

  • meridiens ('a')
  • timezone ('ZZ')
  • days of the week ('dddd' and its variants)

Let me know which option you'd like to take, and I'll have a go at it (probably by the end of this week if I'm not too busy). I'm voting B for consistency's sake with other .format() implementations.

@iamkun
Copy link
Owner

iamkun commented Nov 10, 2020

@verzac Thanks

In general, the duration().format() should accepts format tokens much alike dayjs().format() https://day.js.org/docs/en/display/format, and just ignore those tokens that are less related to duration like Z / 'A'.

I'd like to see the result of example B, cause it causes less confusion. If someone wants to get '75', they could use asMinutes instead.

Something I'm still thinking about is how to display format('YYYY'). If we display it like '1 year, 2 years', should we consider i18n for this as well?

@verzac
Copy link
Contributor

verzac commented Nov 10, 2020

If we display it like '1 year, 2 years', should we consider i18n for this as well?

@iamkun Interesting... Wouldn't this fall under .humanize()'s responsibility (i.e. to display things prettily for the human eye)? I think people using .format() would like to finely tune how their duration representation is formatted, so it's best to not try to guess what kind of string they'd prefer, or what language/terminologies they want to use (e.g. '1 year' or '1 years', or perhaps '1 revolution of the sun') and just chuck the number straight into formatStr.

Otherwise, sounds good to me 👍

@iamkun
Copy link
Owner

iamkun commented Nov 11, 2020

@verzac Oh, yes. Y is just a token for the number of the year. Forget me.

@iamkun
Copy link
Owner

iamkun commented Dec 5, 2020

🎉 This issue has been resolved in version 1.9.7 🎉

The release is available on:

Your semantic-release bot 📦🚀

@tejasupmanyu
Copy link

Hi! can we remove zero values from formatted result? for example - dayjs.duration(1500, 'seconds').format('H[h] m[m] s[s]')

gives 0h 25m 0s

I'd like it to be just 25m

@JBtje
Copy link

JBtje commented Nov 29, 2021

Hi! can we remove zero values from formatted result? for example - dayjs.duration(1500, 'seconds').format('H[h] m[m] s[s]')

gives 0h 25m 0s

I'd like it to be just 25m

I second this. Momentjs also doesn't show the prefix 0 values (it does show the suffic zeroes). This is the only problem i have while converting to dayjs.

e.g.:
dayjs.duration(3600, 'seconds').format('H[h] m[m] s[s]')
1h 0m 0s

dayjs.duration(60, 'seconds').format('H[h] m[m] s[s]')
1m 0s

dayjs.duration(59, 'seconds').format('H[h] m[m] s[s]')
59s

@Tragio
Copy link

Tragio commented Jan 1, 2022

@iamkun would be possible to reopen this issue? Happy new year 🚀

@davorian
Copy link

davorian commented Feb 16, 2022

@Tragio tejasupmanyu dayjs
.duration(365 * 24 * 60 * 60 * 1000)
.format(Y[y] M[m])
.replace(/\b0y\b/, '')
.replace(/\b0m\b/, '')
.replace(/\b0d\b/, '')
.replace(/\b0h\b/, '')

would remove 0y,0m,0d, etc, if they are present.

@SalahAdDin
Copy link

Hi! can we remove zero values from formatted result? for example - dayjs.duration(1500, 'seconds').format('H[h] m[m] s[s]')

gives 0h 25m 0s

I'd like it to be just 25m

THANK YOU!

@dteunkenstt
Copy link

dteunkenstt commented May 13, 2022

@SalahAdDin Just came across this thread.
Here's what I'm using, but I'm sure you've already managed to do it by now.
return formattedDuration.replace(/\b0+[a-z]+\s*/gi, '').trim();

Works for H[h] m[m] s[s], but also HH[h] mm[m] ss[s]

@BeniFreitag
Copy link

BeniFreitag commented Aug 9, 2022

Would be useful if duration could be formatted like in moment-duration-format, e.g. y[y] w[w] d[d] h[h] m[m]. Is there anything similar or planned, especially to format as weeks?

@innovaweb-dev
Copy link

If is possible to get duration of over 24 hours ?

In my case, I must do the sum of decimal hours

dayjs.duration(value,'hours' ).format('H[h] m[m]')
/*For example
47.5 = 47H30
30.2 = 30H12 
*/

But dayjs returns

  • for 47.5 -> 0H30
  • for 30.2 ->7H12

@ShakurOo
Copy link

A possible workaround :

export const formatDuration = (milliseconds: number): string => {
  const dayJSduration = dayjs.duration(milliseconds, 'milliseconds');
  const nbDays = dayJSduration.get('day');
  const nbMinutes = dayJSduration.get('minute');
  const dynamicFormats = [!!nbDays && 'H[h]', !!nbMinutes && 'm[m]']
    .filter(Boolean)
    .join(' ');

  return dayJSduration.format(dynamicFormats);
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request released
Projects
None yet
Development

Successfully merging a pull request may close this issue.