andrewplummer / Sugar

A Javascript library for working with native objects.
https://sugarjs.com/
MIT License
4.53k stars 305 forks source link

How to custom format a time range #656

Open subz390 opened 4 years ago

subz390 commented 4 years ago
andrewplummer commented 4 years ago

Hi, sorry for the delay... I'm showing Date.range('00:00', '04:30').hours() as working fine... a format method on a Date range isn't a thing now but it certainly could be! I'll mark this as an idea for a new feature.

subz390 commented 4 years ago

no problem on the delay, thanks for responding. Of course I could use just hours, just minutes or just seconds. But in a real world application, for example a countdown timer would look silly displaying something like there is 24 hrs 600 min and 12654 seconds remaining it doesn't work like that. Can you imagine a marathon display board, you finished in 2hrs 934 min and 522345 seconds lol!!! You'd expect it to be like there is 24hrs 15 min and 5 seconds remaining. Maybe I'm missunderstanding something about how to use the library but I don't see any way to get the formatted output like that. So currently I get the time remaining result in seconds and then use another function to calculate the resulting hours minutes and seconds. Leaving out any zero results, like when we're down to less than 1 hour it puts out just the min and sec.

andrewplummer commented 4 years ago

Gotcha... well in that regard all I can say is that the DateRange object itself is maybe not as mature as it potentially could be, and I'll definitely take this use case into consideration for the next iteration. For now you I'd say you can achieve what you want with some app-side logic and I'll see what I can do about taking this use case into consideration for an upcoming version... but to answer your immediate question yes for now you're going to have to roll this yourself...