Temporal.PlainTime.prototype.toString()
toString()
方法用于 Temporal.PlainTime
实例,以 RFC 9557 格式 返回表示该时间的字符串。
语法
js
toString()
toString(options)
参数
options
可选-
一个包含以下部分或全部属性的对象(按检索和验证的顺序):
fractionalSecondDigits
可选-
一个 0 到 9 之间的整数,或字符串
"auto"
。默认值为"auto"
。如果为"auto"
,则从小数秒中删除尾随零。否则,秒组件的小数部分包含这么多位数,必要时用零填充或四舍五入。 roundingMode
可选-
一个字符串,指定如何对超出
fractionalSecondDigits
的小数秒位数进行四舍五入。请参阅Intl.NumberFormat()
。默认为"trunc"
。 smallestUnit
可选-
一个字符串,指定输出中包含的最小单位。可能的值是
"minute"
、"second"
、"millisecond"
、"microsecond"
和"nanosecond"
,或它们的复数形式,它们(除了"minute"
)分别等同于fractionalSecondDigits
值为0
、3
、6
、9
。如果指定,则忽略fractionalSecondDigits
。
返回值
以 RFC 9557 格式 表示该时间的字符串。
异常
RangeError
-
如果任何选项无效,则抛出。
TypeError
-
如果
options
不是对象或undefined
,则抛出错误。
示例
使用 toString()
js
const time = Temporal.PlainTime.from("12:34:56");
console.log(time.toString()); // '12:34:56'
使用选项
js
const time1 = Temporal.PlainTime.from("12:00:00");
console.log(time1.toString()); // '12:00:00'
console.log(time1.toString({ fractionalSecondDigits: 1 })); // '12:00:00.0'
console.log(time1.toString({ smallestUnit: "minute" })); // '12:00'
console.log(time1.toString({ smallestUnit: "nanosecond" })); // '12:00:00.000000000'
const time2 = Temporal.PlainTime.from("12:34:56.123456789");
console.log(time2.toString({ fractionalSecondDigits: 4 })); // '12:34:56.1234'
console.log(
time2.toString({ fractionalSecondDigits: 4, roundingMode: "halfExpand" }),
); // '12:34:56.1235'
规范
规范 |
---|
Temporal # sec-temporal.plaintime.prototype.tostring |
浏览器兼容性
加载中…