-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Comments
Will add this later. |
@iamkun , Please let me know once this issue is fixed |
@iamkun let me know too. |
We need it too. |
Yes, please. I don't wanna do maths. |
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 |
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?
In addition, do we want the API to do a mix of both A and B, depending on formatStr? i.e.:
Also, unlike the normal format, I'd probably ignore:
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. |
@verzac Thanks In general, the I'd like to see the result of example B, cause it causes less confusion. If someone wants to get '75', they could use 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? |
@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 👍 |
@verzac Oh, yes. Y is just a token for the number of the year. Forget me. |
🎉 This issue has been resolved in version 1.9.7 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Hi! can we remove zero values from formatted result? for example - gives I'd like it to be just |
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(60, 'seconds').format('H[h] m[m] s[s]') dayjs.duration(59, 'seconds').format('H[h] m[m] s[s]') |
@iamkun would be possible to reopen this issue? Happy new year 🚀 |
@Tragio tejasupmanyu dayjs would remove 0y,0m,0d, etc, if they are present. |
THANK YOU! |
@SalahAdDin Just came across this thread. Works for H[h] m[m] s[s], but also HH[h] mm[m] ss[s] |
Would be useful if duration could be formatted like in moment-duration-format, e.g. |
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
|
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);
}; |
I have tried o use this but it does not work
The text was updated successfully, but these errors were encountered: