bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/JavaScript/Objects, Classes, and Advanced Patterns
JavaScript•Objects, Classes, and Advanced Patterns

JavaScript Temporal PlainTime

The PlainTime Object

The

Temporal.PlainTime object is a time object without a date.

Formula

It represents an ISO 8601 wall - clock time without a date or time zone.

Example:

10:30:00.

Formula

It is useful for opening hours, alarms, and any time - only values.

The Temporal.PlainTime Object

The

Temporal PlainTime object is a time object with no date.

Formula

It represents an ISO 8601 wall - clock time without a date or time zone.

Example:

14:30:00.

Example

const date = new Temporal.PlainTime(14, 30);

Create a PlainTime

You can create a PlainTime from a string.

Example

const time = Temporal.PlainTime.from("14:30");

You can also create a PlainTime using numeric values.

Example

const time = new Temporal.PlainTime(14, 30);

The parameters are: hour, minute, second, millisecond, microsecond, nanosecond.

Get the Current Time

You can get the current local time using Temporal.Now.plainTimeISO().

Example

const now = Temporal.Now.plainTimeISO();

Get Time Parts

You can read the different parts of a PlainTime value.

Example

const time = Temporal.PlainTime.from("14:30:15.123");

Add and Subtract Time

Use add()

and subtract() to do time math. PlainTime is immutable, so the original value is not changed.

Example

const time = Temporal.PlainTime.from("14:30");
const later = time.add({ minutes: 45 });
const earlier = time.subtract({ hours: 2 });

If the time passes midnight, it wraps around:

Example

const time = Temporal.PlainTime.from("23:30");
const result = time.add({ minutes: 90 });

JavaScript Temporal since()

The since()

method calculates the duration between two temporal dates. Syntax t1.since( t2, options )

Meaning:

At time t1, how much time has passed since time t2 ?

Example

const start = Temporal.PlainTime.from("09:00");
const end = Temporal.PlainTime.from("17:30");
const duration = end.since(start);

The since()

method returns a Temporal.Duration Object representing the elapsed time. The duration is positive if the "other" date is in the past. The duration is negative if the "other" date is in the future.

Compare PlainTime Values

Use

Temporal.PlainTime.compare() to compare times.

Previous

JavaScript Closures

Next

JavaScript Function Reference