如何在Moment.js中格式化持续时间。

huangapple go评论91阅读模式
英文:

how to format Duration in momentjs

问题

I made a countdown clock which shows how many years, months, days, hours, minutes and seconds left until certain moment in time.

I just have a small issue solution to which should be obvious enough but I just can't figure it out for the life of me. How can I format data that comes out of timeBetween.hours() and timeBetween.seconds()? I need them to always be of 2-digit format but by default if a number is less than 10 it's not padded with another zero.

format() function doesn't work with duration. I found a workaround by formatting moment(timebetween)._data but then I have a month off since it becomes index based.

Here's the code:

  1. const Clock = () => {
  2. const [clock, setClock] = useState(moment());
  3. const theEnd = moment('2050-01-01 00:00:00');
  4. const timeBetween = moment.duration(theEnd.diff(clock));
  5. useEffect(() => { setInterval(() => setClock(moment()), 1000)}, []);
  6. return (
  7. <div className="clock-container">
  8. <p className='time'>{`${timeBetween.years()} years, ${timeBetween.months()} months, ${timeBetween.days()} days`}</p>
  9. <p className='time'>{`${timeBetween.hours().toString().padStart(2, '0')} hours : ${timeBetween.minutes().toString().padStart(2, '0')} minutes : ${timeBetween.seconds().toString().padStart(2, '0')} seconds`}</p>
  10. </div>
  11. );
  12. };
  13. export default Clock;
英文:

I made a countdown clock which shows how many years, months, days, hours, minutes and seconds left until certain moment in time.

I just have a small issue solution to which should be obvious enough but I just can't figure it out for the life of me. How can I format data that comes out of timeBetween.hours() and timeBetween.seconds()? I need them to always be of 2-digit format but by default if a number is less than 10 it's not padded with another zero.

format() function doesn't work with duration. I found a workaround by formatting moment(timebetween)._data but then I have a month off since it becomes index based.

Here's the code:

  1. const Clock = () =&gt; {
  2. const [clock, setClock] = useState(moment());
  3. const theEnd = moment(&#39;2050-01-01 00:00:00&#39;);
  4. const timeBetween = moment.duration(theEnd.diff(clock));
  5. useEffect(() =&gt; { setInterval(() =&gt; setClock(moment()), 1000)}, []);
  6. return (
  7. &lt;div className=&quot;clock-container&quot;&gt;
  8. &lt;p className=&#39;time&#39;&gt;{`${timeBetween.years()} years, ${timeBetween.months()} months, ${timeBetween.days()} days`}&lt;/p&gt;
  9. &lt;p className=&#39;time&#39;&gt;{`${timeBetween.hours()} hours : ${timeBetween.minutes()} minutes : ${timeBetween.seconds()} seconds`}&lt;/p&gt;
  10. &lt;/div&gt;
  11. );
  12. };
  13. export default Clock;

答案1

得分: 0

我用这种方式填充它。简单至极。

  1. const zeroPad = (num: number) => String(num).padStart(2, '0');
  2. <div className="clock-container">
  3. <p className='time'>{`${timeBetween.years()}年,${timeBetween.months()}个月,${timeBetween.days()}天`}</p>
  4. <p className='time'>{`${zeroPad(timeBetween.hours())}小时:${zeroPad(timeBetween.minutes())}分钟:${zeroPad(timeBetween.seconds())}秒`}</p>
  5. </div>
英文:

I padded it like this. The simplicity is overwhelming.

  1. const zeroPad = (num: number) =&gt; String(num).padStart(2, &#39;0&#39;);
  2. &lt;div className=&quot;clock-container&quot;&gt;
  3. &lt;p className=&#39;time&#39;&gt;{`${timeBetween.years()} years, ${timeBetween.months()} months, ${timeBetween.days()} days`}&lt;/p&gt;
  4. &lt;p className=&#39;time&#39;&gt;{`${zeroPad(timeBetween.hours())} hours : ${zeroPad(timeBetween.minutes())} minutes : ${zeroPad(timeBetween.seconds())} seconds`}&lt;/p&gt;
  5. &lt;/div&gt;

huangapple
  • 本文由 发表于 2023年4月19日 21:32:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76055171.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定