You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Temporal.Duration对象格式化为hh:mm:ss格式字符串?关于可用方法及Temporal.DurationFormatOptions文档的咨询

Formatting Temporal.Duration as hh:mm:ss String

First, let's clarify why your diff.hours and diff.minutes are returning 0: when you calculate the time difference between two Temporal.Instants with until(), the resulting Duration defaults to using the smallest possible unit to represent the total duration. In your case, the full time gap is stored as 86790 seconds, so hours and minutes remain 0 while seconds holds the total value.

Here are a few reliable ways to format this into the hh:mm:ss format you need:

1. Use Temporal.Duration.round() to decompose units first

The cleanest approach is to leverage Temporal's built-in round() method to break down the total seconds into hours, minutes, and seconds. This avoids manual math and uses Temporal's robust time unit handling:

console.clear();
const locale = "en-US";
// Fixed the Instant format to comply with Temporal's required ISO 8601 syntax
const start = Temporal.Instant.from('2026-01-28T09:00:00.300000Z'); 
const finish = Temporal.Now.instant();
console.log(`from "${start.toLocaleString(locale)}" to ${finish.toLocaleString(locale)}`);

const diff = start.until(finish);
// Round the duration to decompose into hours, minutes, and seconds
const decomposedDiff = diff.round({
  largestUnit: 'hours',
  smallestUnit: 'seconds'
});

// Format with leading zeros for consistent hh:mm:ss structure
const formattedDuration = `${String(decomposedDiff.hours).padStart(2, '0')}:${String(decomposedDiff.minutes).padStart(2, '0')}:${String(decomposedDiff.seconds).padStart(2, '0')}`;
console.log(formattedDuration); // Example output: "24:06:30"

2. Manual calculation from total seconds

If you prefer to handle the math directly (or need compatibility with older environments), you can compute the units yourself from the total seconds value:

const totalSeconds = diff.seconds;
const hours = Math.floor(totalSeconds / 3600);
const remainingSeconds = totalSeconds % 3600;
const minutes = Math.floor(remainingSeconds / 60);
const seconds = remainingSeconds % 60;

const formattedDuration = `${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
console.log(formattedDuration);

3. Use Temporal.PlainTime for durations under 24 hours

If you're certain your duration will never exceed 24 hours, you can add the duration to a zero-time PlainTime and format the result:

const baseTime = Temporal.PlainTime.from('00:00:00');
const durationAsTime = baseTime.add(diff);
const formattedDuration = durationAsTime.toLocaleString(locale, {
  hour: '2-digit',
  minute: '2-digit',
  second: '2-digit',
  hourCycle: 'h23' // Ensures 24-hour format instead of AM/PM
});
console.log(formattedDuration); // Example output: "00:06:30" for a 24h6m30s duration (wraps past 24h)

⚠️ Important note: This method wraps durations over 24 hours (e.g., 25 hours becomes 01:00:00), so only use it for time gaps shorter than a full day.

As for Temporal.DurationFormatOptions, the toLocaleString() method for Duration does support formatting options, but it doesn't directly produce the compact hh:mm:ss format. It can display units in long/short/narrow styles (e.g., "24 hours, 6 minutes, 30 seconds"), but not the colon-separated structure you're looking for.

内容的提问来源于stack exchange,提问作者michaelT

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 10:42:35