ts-time/LocalTime

Consumption

import LocalTime, {MIN_TIME, MAX_TIME, MAX_TIME12, MIDNIGHT, NOON} from "ts-time/LocalTime";

Default export#

Hierarchy#

  • class LocalTime

Description#

Comprises a specific time, without date and time zone relation. In other words, this is a tuple of (hour, minute, second, millisecond), or a number of milliseconds since day start (from 0 inclusive to MS_PER_DAY exclusive).
Construct#

A common way to construct an object in ts-time is to call its of static method. For example, the following LocalTime instance represents 18 hours, 30 minutes, 15 seconds, 225 milliseconds:

LocalTime.of(18, 30, 15, 225);

As opposed to Java API, there's no common way to get the current instance of an object in ts-time, because ts-time doesn't have a concept of default time zone. Instead, you must get the current Instant and convert it to an object that you need, taking time zone into consideration.

Instant.now().atZone(UTC).time;             // Current LocalTime in UTC
Instant.now().atZone(LOCAL_ZONE_ID).time;   // Current LocalTime in the local time zone
Instant.now().atZone(zone).time;            // Current LocalTime in a given ZoneId/ZoneOffset

It makes ts-time API more robust. It leaves less room for a mistake.

A common way to convert a native JavaScript Date object to a ts-time object is to call fromNative* static method:

LocalTime.fromNativeUtc(date);              // LocalTime from a given Date in UTC
LocalTime.fromNativeLocal(date);            // LocalTime from a given Date in the local time zone
Instant.fromNative(date).atZone(zone).time; // LocalTime from a given Date in a given ZoneId/ZoneOffset

You can convert instances of other ts-time classes to LocalTime via their properties and methods:

instant.atZone(zone).time;                  // From Instant in a given ZoneId/ZoneOffset
localDateTime.time;                         // From LocalDateTime
offsetDateTime.time;                        // From OffsetDateTime in the same ZoneOffset
zonedDateTime.time;                         // From ZonedDateTime in the same ZoneId

Please notice that all these conversions result in a partial data loss (date, time zone, offset).

For some commonly used time moments, there are predefined LocalTime instances: MIDNIGHT, NOON, MIN_TIME, MAX_TIME, MAX_TIME12.

Another sophisticated constructor: ofTotalMs.

Parse#

A common way to parse an ISO 8601 compliant string in ts-time is to call parse static method. For example, the following LocalTime instance represents 18 hours, 30 minutes, 15 seconds, 225 milliseconds:

LocalTime.parse("18:30:15.225");

The library doesn't yet support parsing non-compliant strings.

Inspect#

In the following example, we inspect various properties of a LocalTime object:

const time   = LocalTime.of(18, 30, 15, 225);
const hour   = time.hour;   // 18
const minute = time.minute; // 30
const second = time.second; // 15
const ms     = time.ms;     // 225

Another sophisticated feature for LocalTime inspection: totalMs.

Compare#

A common way to compare objects in ts-time is to call equals, isBefore, isAfter, compareTo methods:

const t1 = LocalTime.of(18, 30, 15, 225);
const t2 = LocalTime.of(18, 30, 15, 226);
t1.equals(t2);    // false
t1.isBefore(t2);  // true
t1.isAfter(t2);   // false
t1.compareTo(t2); // -1

For nullable objects, use static methods instead. Null and undefined are considered less than anything, except each other:

const t1: LocalTime = null;
const t2 = LocalTime.of(18, 30, 15, 225);
LocalTime.equal(t1, t2);     // false
LocalTime.isBefore(t1, t2);  // true
LocalTime.isAfter(t1, t2);   // false
LocalTime.compare(t1, t2);   // -1
Manipulate#

Every object in ts-time is immutable. Therefore every manipulation returns a new object.

To add/subtract a Duration, call plus/minus method. Please notice that the result is always between 00:00:00.000 inclusive and 24:00:00.000 exclusive:

const time = LocalTime.of(18, 30, 15, 225);
const t1 = time.plus(MINUTE_DURATION);         // 18:31:15.225
const t2 = time.plus(Duration.ofHours(10));    // 04:30:15.225
const t3 = time.minus(Duration.ofSeconds(30)); // 18:29:45.225

If you need to take full days (positive or negative) into consideration, use Duration or LocalDateTime instead of LocalTime.

To change one of the components, preserving all the rest, call with* methods:

const time = LocalTime.of(18, 30, 15, 225);
const t1 = time.withHour(20);                  // 20:30:15.225
const t2 = time.withMinute(20);                // 18:20:15.225
const t3 = time.withSecond(20);                // 18:30:20.225
const t4 = time.withMs(20);                    // 18:30:15.020

To truncate all the least-significant components, call truncate* methods:

const time = LocalTime.of(18, 30, 15, 225);
const t1 = time.truncateToHour;                // 18:00:00.000
const t2 = time.truncateToMinute;              // 18:30:00.000
const t3 = time.truncateToSecond;              // 18:30:15.000
Convert#

You can convert LocalTime to other kinds of objects via its methods:

const time = LocalTime.of(18, 30, 15, 225);
time.atDate(date);                      // To LocalDateTime on a given LocalDate
time.atDate(date).atOffset(offset);     // To OffsetDateTime on a given LocalDate in a given ZoneOffset
time.atDate(date).atZone(zone);         // To ZonedDateTime on a given LocalDate in a given ZoneId
time.atDate(date).atZone(zone).instant; // To Instant on a given LocalDate in a given ZoneId/ZoneOffset

In order to convert LocalTime from one ZoneId to another, you must explicitly specify the date of conversion (as certain time zones have varying offsets over the time line), get an Instant and then convert it to the target ZoneId:

time.atDate(date).atZone(sourceZone).instant.atZone(targetZone).time;

Conversion of ZoneOffset doesn't depend on a date:

time.minus(Duration.ofSeconds(sourceOffset.totalSeconds)).plus(Duration.ofSeconds(targetOffset.totalSeconds));

You can as well convert LocalTime to a native JavaScript Date, provided you know a LocalDate and a ZoneId/ZoneOffset:

const time = LocalTime.of(18, 30, 15, 225);
LocalDateTime.of(date, time).atZone(zone).native; // Date representing 18:30:15.225 on a given LocalDate in a given ZoneId/ZoneOffset

For backward conversion, see Construct.

Format#

Every class in ts-time has ISO 8601 compliant toString method:

const time = LocalTime.of(18, 30, 15, 225);
time.toString(); // "18:30:15.225"

For more sophisticated string formatting, add ts-time-format library to your list of dependencies:

npm install --save ts-time-format

Now you can construct an instance of TimeFormatter in order to format arbitrary LocalTime instances:

const formatter = TimeFormatter.ofPattern("hh:mm a");
const time = LocalTime.of(18, 30, 15, 225);
formatter.format(time); // "06:30 PM"

Fields#

totalMs#

readonly totalMs: number

Total number of milliseconds since day start (from 0 inclusive to MS_PER_DAY exclusive).
hour#

readonly hour: number

Hour of a day (0-23).
minute#

readonly minute: number

Minute of an hour (0-59).
second#

readonly second: number

Second of a minute (0-59).
ms#

readonly ms: number

Millisecond of a second (0-999).
truncateToHour#

readonly truncateToHour: LocalTime

Start of the hour (i.e. 0 minutes, 0 seconds, 0 milliseconds).
truncateToMinute#

readonly truncateToMinute: LocalTime

Start of the minute (i.e. 0 seconds, 0 milliseconds).
truncateToSecond#

readonly truncateToSecond: LocalTime

Start of the second (i.e. 0 milliseconds).

Methods#

get#

(field: TimeField): number

field
Field to get.
returns
Value of the specified field.

This is an experimental API. There's a higher chance that the authors of the library may remove or change it in future releases.

compareTo#

(other: LocalTime): number

other
Local time to compare to.
returns
  • 0 if this is the same local time;
  • positive value if this local time goes after other;
  • negative value if this local time goes before other.
Null and undefined are considered less than anything, except each other.

Note that a method call on null or undefined always leads to an error. So, if your variable may contain null or undefined, use the respective static method instead.

equals#

(other: LocalTime): boolean

other
Local time to compare to.
returns
True if this local time is equal to other. Null and undefined are only equal to each other.

Note that a method call on null or undefined always leads to an error. So, if your variable may contain null or undefined, use the respective static method instead.

isBefore#

(other: LocalTime): boolean

other
Local time to compare to.
returns
True if this local time goes before other. Null and undefined go before anything, except each other.

Note that a method call on null or undefined always leads to an error. So, if your variable may contain null or undefined, use the respective static method instead.

isAfter#

(other: LocalTime): boolean

other
Local time to compare to.
returns
True if this local time goes after other. Null and undefined go before anything, except each other.

Note that a method call on null or undefined always leads to an error. So, if your variable may contain null or undefined, use the respective static method instead.

atDate#

(date: LocalDate): LocalDateTime

date
Local date.
returns
This time on the specified date.
plus#

(duration: Duration): LocalTime

duration
Duration to add.
returns
New LocalTime shifted onwards from this one by the specified duration.
minus#

(duration: Duration): LocalTime

duration
Duration to subtract.
returns
New LocalTime shifted backwards from this one by the specified duration.
withHour#

(hour: number): LocalTime

hour
Hour of a day.
returns
New LocalTime with the specified hour of a day. All other properties don't change.
withMinute#

(minute: number): LocalTime

minute
Minute of an hour.
returns
New LocalTime with the specified minute of an hour. All other properties don't change.
withSecond#

(second: number): LocalTime

second
Second of a minute.
returns
New LocalTime with the specified second of a minute. All other properties don't change.
withMs#

(ms: number): LocalTime

ms
Millisecond of a second.
returns
New LocalTime with the specified millisecond of a second. All other properties don't change.
toString#

(): string

returns
String representation of the time in ISO format, such as "18:30:15.225".

Static methods#

of#

(hour: number, minute: number = 0, second: number = 0, ms: number = 0): LocalTime

hour
Hour of day.
minute
Minute of hour.
second
Second of minute.
ms
Millisecond of second.
returns
LocalTime representation.
ofTotalMs#

(totalMs: number): LocalTime

totalMs
Total number of milliseconds since start of the day.
returns
LocalTime representation.
fromNativeLocal#

(date: Date): LocalTime

date
Native JS date.
returns
LocalTime representation of time of date in the browser time zone, for compatibility with native and third party API.
fromNativeUtc#

(date: Date): LocalTime

date
Native JS date.
returns
LocalTime representation of time of date in UTC, for compatibility with native and third party API.
parse#

(str: string): LocalTime throws TemporalParsingError

str
String representation of a local time in ISO format, such as '18:30:15.225'.
returns
LocalTime representation.
compare#

(x: LocalTime, y: LocalTime): number

x
One local time.
y
Another local time.
returns
  • 0 if this x and y are the same local dates;
  • positive value if x goes after y;
  • negative value if x goes before y.
Null and undefined are considered less than anything, except each other.
equal#

(x: LocalTime, y: LocalTime): boolean

x
One local time.
y
Another local time.
returns
True if x is equal to y. Null and undefined are only equal to each other.
isBefore#

(x: LocalTime, y: LocalTime): boolean

x
One local time.
y
Another local time.
returns
True if x goes before y. Null and undefined go before anything, except each other.
isAfter#

(x: LocalTime, y: LocalTime): boolean

x
One local time.
y
Another local time.
returns
True if x goes after y. Null and undefined go before anything, except each other.

MIN_TIME#

MIN_TIME: LocalTime

Minimal LocalTime (00:00:00.000).

MAX_TIME#

MAX_TIME: LocalTime

Maximum LocalTime (23:59:59.999).

MAX_TIME12#

MAX_TIME12: LocalTime

Maximum LocalTime in 12-hour system (11:59:59.999).

MIDNIGHT#

MIDNIGHT: LocalTime

Midnight (00:00:00.000).

NOON#

NOON: LocalTime

Noon (12:00:00.000).