mckamey / countdownjs

A simple JavaScript API for producing an accurate, intuitive description of the timespan between two Date instances.
http://countdownjs.org
MIT License
496 stars 95 forks source link

Zero pad milliseconds with 3 digits? #29

Open jetersen opened 6 years ago

jetersen commented 6 years ago

Sorry for not spending more time.

I would like to zero pad weeks, days, hours, minutes with two digits and then zero pad milliseconds with 3 digits.

So you would get below output from the countdown().toString

01 day, 09 hours, 05 minutes, 03 seconds and 005 milliseconds 08 days, 02 hours, 09 minutes, 16 seconds and 095 milliseconds

Perhaps the setLabels format function could provide number and unit of said number to then zero pad differently

jetersen commented 6 years ago

@mckamey Sorry for not being more detailed. I was asking a question and would be happy to implement it myself if you asked me to do so if it was not currently possible. I have added more details to the question. I honestly thought the title was enough.

jetersen commented 6 years ago

I ended up avoiding using the toString or toHTML and just read the values off the timespan object.

jetersen commented 6 years ago

Using toString method or toHTML method on a timespan object I would like to pad numbers with zero.

Milliseconds should always have a length of three digits. while the rest of the units should always have a length of two digits. So that countdown().toString always shows 50 milliseconds as 050 milliseconds or 5 weeks as 05.

mckamey commented 6 years ago

As for custom formatting, including padding with zeros, you can pass in a callback like this to do your formatting:

https://github.com/mckamey/countdownjs/issues/4#issuecomment-109644816

mckamey commented 6 years ago

Or even better, there is a formatter function option here:

https://github.com/mckamey/countdownjs/blob/master/countdown.js#L1300-L1305

I knew I had added something like that but it looks like the readme wasn't updated.

jetersen commented 6 years ago

Well formatter callback only gives me the value so the function I provide applies to all values, and what am fishing after is being able to retrieve the unit type of the value being formatted so I can write a function that can treat them differently.

mckamey commented 6 years ago

In the options formatNumber receives only value but formatter gets value and units.

jetersen commented 6 years ago

@mckamey awesome 😅 The readme does mention the formatter but not that it returns value and units 👍

If you like I can send a PR that updates the text about formatNumber and formatter. Noticed that the readme mentions the formatter in the context of setLabels which is deprecated since version 2.6.0.

https://github.com/mckamey/countdownjs/blob/cb5b1877ca474d3c86e0407c090a7af90fbded41/countdown.js#L1322-L1344

https://github.com/mckamey/countdownjs/blob/master/README.md#localization formatter is a function which takes a number and returns a string (default uses Number.toString()), allowing customization of the way numbers are formatted, e.g., commas every 3 digits or some unique style that is specific to your locale.

mckamey commented 6 years ago

Yeah those are now set via countdown.setFormat / countdown.resetFormat, since it does more than labels.

A PR updating the readme would be great. Thanks.