如何在时间中显示毫秒或微秒?
使用 Intl.DateTimeFormat 显示时间的亚秒级精度
介绍
大多数时间显示会显示小时、分钟和秒。这种精确度适用于日程安排、预约和日常时间记录。然而,一些应用程序需要亚秒级精度来显示十分之一、百分之一或千分之一秒。
性能监控工具以毫秒为单位显示响应时间。科学应用程序以亚秒级精度显示测量值。视频和音频应用程序以毫秒为单位显示时间戳。日志系统使用小数秒时间戳记录事件,以区分间隔很近的事件。
JavaScript 的 Intl.DateTimeFormat 提供了 fractionalSecondDigits 选项来显示亚秒级精度。本课程将解释什么是小数秒、如何显示它们以及何时需要这种精度。
理解小数秒精度
秒是时间测量的基本单位。小数秒表示小于一整秒的部分。
十分之一秒等于 0.1 秒,也就是 100 毫秒。这种精度可以区分在十分之一秒内发生的事件。
百分之一秒等于 0.01 秒,也就是 10 毫秒。这种精度可以区分在百分之一秒内发生的事件。
千分之一秒等于 0.001 秒,也就是 1 毫秒。这是 Intl.DateTimeFormat 支持的最高精度。
JavaScript 通过小数点后的数字表示小数秒。一位数字表示十分之一秒,两位数字表示百分之一秒,三位数字表示千分之一秒。
关于微秒
课程标题提到了微秒,但 Intl.DateTimeFormat 不支持微秒精度。微秒表示百万分之一秒,需要小数点后六位数字。该 API 支持最多三位数字,也就是毫秒精度。
JavaScript 的 Date 对象内部以自 1970 年 1 月 1 日 UTC 起的毫秒数存储时间。这意味着底层数据支持毫秒精度,但不支持微秒精度。
使用 fractionalSecondDigits 选项
fractionalSecondDigits 选项控制秒字段中小数点后的位数。它接受 1、2 或 3 的值。
在使用 fractionalSecondDigits 时,必须包含 second 选项。如果没有 second 选项,格式化器会完全省略秒字段,fractionalSecondDigits 选项将不起作用。
const date = new Date('2025-10-15T14:23:45.678');
const formatter = new Intl.DateTimeFormat('en-US', {
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
fractionalSecondDigits: 3
});
console.log(formatter.format(date));
// 输出: "2:23:45.678 PM"
此格式化器显示小时、分钟、秒和三位小数秒。Date 对象包含毫秒精度,格式化器会显示所有三位数字。
显示一位小数秒
将 fractionalSecondDigits 设置为 1 会显示十分之一秒。
const date = new Date('2025-10-15T14:23:45.678');
const formatter = new Intl.DateTimeFormat('en-US', {
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
fractionalSecondDigits: 1
});
console.log(formatter.format(date));
// 输出: "2:23:45.6 PM"
格式化器将值四舍五入到一位小数。原始值包含 678 毫秒,相当于 0.678 秒,显示为一位数字时四舍五入为 0.6 秒。
显示两位小数秒
将 fractionalSecondDigits 设置为 2 会显示百分之一秒。
const date = new Date('2025-10-15T14:23:45.678');
const formatter = new Intl.DateTimeFormat('en-US', {
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
fractionalSecondDigits: 2
});
console.log(formatter.format(date));
// 输出: "2:23:45.67 PM"
格式化器将值四舍五入到两位小数。值 678 毫秒相当于 0.678 秒,显示为两位数字时四舍五入为 0.67 秒。
显示三位小数秒
将 fractionalSecondDigits 设置为 3 会显示千分之一秒,也就是毫秒。
const date = new Date('2025-10-15T14:23:45.678');
const formatter = new Intl.DateTimeFormat('en-US', {
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
fractionalSecondDigits: 3
});
console.log(formatter.format(date));
// 输出: "2:23:45.678 PM"
这显示了 JavaScript Date 对象中可用的完整毫秒精度。三位数字是 Intl.DateTimeFormat 支持的最大精度。
与 24 小时制结合
分秒可以与任何时间格式选项一起使用,包括 24 小时制。
const date = new Date('2025-10-15T14:23:45.678');
const formatter = new Intl.DateTimeFormat('en-GB', {
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
fractionalSecondDigits: 3,
hourCycle: 'h23'
});
console.log(formatter.format(date));
// 输出: "14:23:45.678"
英国英语区域默认使用 24 小时制,hourCycle 选项确保了 24 小时格式。分秒以小数点分隔符显示在秒之后。
包含时区和分秒
您可以将分秒与时区信息一起显示。
const date = new Date('2025-10-15T14:23:45.678Z');
const formatter = new Intl.DateTimeFormat('en-US', {
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
fractionalSecondDigits: 3,
timeZone: 'America/New_York',
timeZoneName: 'short'
});
console.log(formatter.format(date));
// 输出: "10:23:45.678 AM EDT"
此格式化器将 UTC 时间转换为纽约时间,并以毫秒精度和时区缩写显示结果。
分秒在不同区域的格式
不同的区域对数字使用不同的小数分隔符,但分秒在所有区域中始终使用句点作为小数分隔符。
const date = new Date('2025-10-15T14:23:45.678');
const options = {
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
fractionalSecondDigits: 3
};
const en = new Intl.DateTimeFormat('en-US', options);
console.log(en.format(date));
// 输出: "2:23:45.678 PM"
const de = new Intl.DateTimeFormat('de-DE', options);
console.log(de.format(date));
// 输出: "14:23:45.678"
const fr = new Intl.DateTimeFormat('fr-FR', options);
console.log(fr.format(date));
// 输出: "14:23:45.678"
const ar = new Intl.DateTimeFormat('ar-SA', options);
console.log(ar.format(date));
// 输出: "٢:٢٣:٤٥٫٦٧٨ م"
英语使用 12 小时制并带有 AM/PM。德语和法语使用 24 小时制。阿拉伯语使用阿拉伯-印度数字,但保持相同的时间格式结构。所有区域对分秒都使用句点或类似的分隔符。
分数秒的常见用例
性能监控以毫秒精度显示响应时间。
const startTime = new Date();
// 执行操作
const endTime = new Date();
const formatter = new Intl.DateTimeFormat('en-US', {
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
fractionalSecondDigits: 3,
hourCycle: 'h23'
});
console.log(`操作完成时间为 ${formatter.format(endTime)}`);
// 输出: "操作完成时间为 14:23:45.678"
这以毫秒精度显示了操作完成的确切时间,用于性能分析。
科学数据记录需要亚秒级时间戳。
function logMeasurement(value) {
const timestamp = new Date();
const formatter = new Intl.DateTimeFormat('en-US', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
fractionalSecondDigits: 3,
hourCycle: 'h23'
});
console.log(`${formatter.format(timestamp)} - 测量值: ${value}`);
}
logMeasurement(23.5);
// 输出: "10/15/2025, 14:23:45.678 - 测量值: 23.5"
这为科学测量创建了具有毫秒精度的时间戳。
视频播放器时间戳以亚秒级精度显示位置。
function formatVideoTimestamp(milliseconds) {
const date = new Date(milliseconds);
const formatter = new Intl.DateTimeFormat('en-US', {
minute: '2-digit',
second: '2-digit',
fractionalSecondDigits: 2,
hourCycle: 'h23',
timeZone: 'UTC'
});
return formatter.format(date);
}
console.log(formatVideoTimestamp(125678));
// 输出: "02:05.67"
这以百分之一秒的精度格式化了视频时间戳。将时区设置为 UTC 可防止对持续时间值进行时区转换。
何时使用分数秒
当需要区分同一秒内发生的事件时,请使用分数秒。性能监控、调试、科学测量和媒体应用通常需要这种精度。
不要在日常时间记录中使用分数秒。约会、日程安排和面向用户的时间戳很少需要亚秒级精度。添加不必要的精度会使时间更难阅读和理解。
根据您的需求选择适当的位数。一位数字为许多用例提供了足够的精度,同时保持可读性。三位数字提供了最大精度,但会生成更长、更具技术感的时间戳。
摘要
fractionalSecondDigits 选项用于在时间格式中显示亚秒级精度。它接受值 1、2 或 3,分别表示显示十分之一秒、百分之一秒或千分之一秒。在使用 fractionalSecondDigits 时,必须包含 second 选项。
JavaScript 的 Intl.DateTimeFormat 支持毫秒级精度,但不支持微秒级精度。三个小数位表示毫秒,这是可用的最高精度。
亚秒与所有其他时间格式选项兼容,包括 12 小时和 24 小时格式、时区以及不同的区域设置。亚秒的小数分隔符在不同区域设置中保持一致。
亚秒可用于性能监控、科学数据、调试日志和媒体时间戳。在日常时间记录中,若秒级精度已足够,则应避免使用亚秒。