ChronosChronos

Extract Time From UTC

Extract Time From UTC — chronos-date API reference.

extractTimeFromUTC

Extracts the time portion from a UTC offset string, converting it to ±HH:MM format.

Function Signature

extractTimeFromUTC(utc: UTCOffset): `-${Time}` | Time;

Parameters

  • utc: A UTC offset string in format:
    • UTC+HH:MM (positive offset)
    • UTC-HH:MM (negative offset)

Returns

The time portion in ±HH:MM format:

  • HH:MM for positive offsets (e.g., "05:30")
  • -HH:MM for negative offsets (e.g., "-04:00")

Example Usage

import { extractTimeFromUTC } from "chronos-date/utils";

// Positive offset
console.log(extractTimeFromUTC("UTC+05:30"));  // "05:30"

// Negative offset
console.log(extractTimeFromUTC("UTC-03:45"));  // "-03:45"

// Zero offset
console.log(extractTimeFromUTC("UTC+00:00"));  // "00:00"

Notes

  • Preserves the original sign (+/-)
  • Returns only the time portion without "UTC" prefix
  • Maintains leading zeros for single-digit hours/minutes
  • Works with all valid UTCOffset formats

Aliases

  • extractTimeStringFromUTC
  • getTimeStringFromUTC

Type Definition

type UTCOffset = `UTC${'+00'|'+01'|...|'+14'|'-00'|'-01'|...|'-14'}:${'00'|'15'|'30'|'45'}`;
type ClockTime = `${ClockHour}:${ClockMinute}`;

Use Cases

  • Displaying timezone offsets without UTC prefix
  • Preparing time data for further processing
  • Formatting timezone information for UI display
  • Timezone conversion utilities

Conclusion

The extractTimeFromUTC function provides:

  1. Clean extraction of time portion from UTC strings
  2. Consistent formatting in ±HH:MM format
  3. Simple integration with time-related operations
  4. Type-safe input/output handling

Ideal for applications requiring:

  • Timezone-aware displays
  • UTC string manipulation
  • Time formatting utilities
  • Internationalization support

On this page