Skip to content

formatTime

The formatTime function formats a time (Date) to a string in the specified format, such as 'HH:mm:ss' or 'HH:mm'.

Syntax

typescript
formatTime(date: Date, pattern?: string): string;

Parameters

NameTypeDescription
dateDateThe date/time to be formatted
patternstring(Optional) Formatting pattern (e.g., HH:mm:ss)

Returns

TypeDescription
stringTime formatted according to the pattern

Examples

typescript
formatTime(new Date("2024-06-10T14:05:09"), 'HH:mm:ss');
// => "14:05:09"

formatTime(new Date("2024-06-10T14:05:09"), 'HH:mm');
// => "14:05"

Notes

  • Throws an error if the date is not valid.
  • Useful for displaying only the time part of a date.

References

Released under the MIT License.