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

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

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:

const Clock = () => {
  const [clock, setClock] = useState(moment());

  const theEnd = moment('2050-01-01 00:00:00');
  const timeBetween = moment.duration(theEnd.diff(clock));
    
  useEffect(() => { setInterval(() => setClock(moment()), 1000)}, []);

  return (
    <div className="clock-container">
      <p className='time'>{`${timeBetween.years()} years, ${timeBetween.months()} months, ${timeBetween.days()} days`}</p>
      <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>
    </div>
  );
};
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:

const Clock = () =&gt; {
const [clock, setClock] = useState(moment());

const theEnd = moment(&#39;2050-01-01 00:00:00&#39;);
const timeBetween = moment.duration(theEnd.diff(clock));

useEffect(() =&gt; { setInterval(() =&gt; setClock(moment()), 1000)}, []);

return (
    &lt;div className=&quot;clock-container&quot;&gt;
        &lt;p className=&#39;time&#39;&gt;{`${timeBetween.years()} years, ${timeBetween.months()} months, ${timeBetween.days()} days`}&lt;/p&gt;
        &lt;p className=&#39;time&#39;&gt;{`${timeBetween.hours()} hours : ${timeBetween.minutes()} minutes : ${timeBetween.seconds()} seconds`}&lt;/p&gt;
    &lt;/div&gt;
);
};
export default Clock;

答案1

得分: 0

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

const zeroPad = (num: number) => String(num).padStart(2, '0');

<div className="clock-container">
    <p className='time'>{`${timeBetween.years()}年,${timeBetween.months()}个月,${timeBetween.days()}天`}</p>
    <p className='time'>{`${zeroPad(timeBetween.hours())}小时:${zeroPad(timeBetween.minutes())}分钟:${zeroPad(timeBetween.seconds())}秒`}</p>
</div>
英文:

I padded it like this. The simplicity is overwhelming.

const zeroPad = (num: number) =&gt; String(num).padStart(2, &#39;0&#39;);

&lt;div className=&quot;clock-container&quot;&gt;
            &lt;p className=&#39;time&#39;&gt;{`${timeBetween.years()} years, ${timeBetween.months()} months, ${timeBetween.days()} days`}&lt;/p&gt;
            &lt;p className=&#39;time&#39;&gt;{`${zeroPad(timeBetween.hours())} hours : ${zeroPad(timeBetween.minutes())} minutes : ${zeroPad(timeBetween.seconds())} seconds`}&lt;/p&gt;
        &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:

确定