javascript instance method
Temporal.PlainTime.prototype.toString()
The toString() method of PlainTime instances returns a string representing this time in the RFC 9557 format.
Syntax
toString()
toString(options)
Parameters
optionsOptional- : An object containing some or all of the following properties (in the order they are retrieved and validated):
fractionalSecondDigitsOptional- : Either an integer from 0 to 9, or the string
"auto". The default is"auto". If"auto", then trailing zeros are removed from the fractional seconds. Otherwise, the fractional part of the second component contains this many digits, padded with zeros or rounded as necessary.
- : Either an integer from 0 to 9, or the string
roundingModeOptional- : A string specifying how to round off fractional second digits beyond
fractionalSecondDigits. SeeIntl.NumberFormat(). Defaults to"trunc".
- : A string specifying how to round off fractional second digits beyond
smallestUnitOptional- : A string specifying the smallest unit to include in the output. Possible values are
"minute","second","millisecond","microsecond", and"nanosecond", or their plural forms, which (except"minute") are equivalent tofractionalSecondDigitsvalues of0,3,6,9, respectively. If specified, thenfractionalSecondDigitsis ignored.
- : A string specifying the smallest unit to include in the output. Possible values are
- : An object containing some or all of the following properties (in the order they are retrieved and validated):
Return value
A string in the RFC 9557 format representing this time.
Exceptions
RangeError- : Thrown if any of the options is invalid.
TypeError- : Thrown if
optionsis not an object orundefined.
- : Thrown if
Examples
Using toString()
const time = Temporal.PlainTime.from("12:34:56");
console.log(time.toString()); // '12:34:56'
Using options
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'
Specifications
SpecificationsStandards references are available on the canonical MDN page.
Browser compatibility
Browser compatibilityCompatibility data is available on the canonical MDN page.