Skip to content

extractTime - Pulls specific time unit from a timestamp

The extractTime function will extract a specific unit of time from a timestamp. For example, pulling out the hour value, or the seconds.

Note

  • Date units such as 'month' or 'week' start from 1 (not from 0).
  • For units smaller than minute, the number is a floating point, otherwise it's an integer.

Syntax

Like many functions in DataPrime, extractTime supports two notations, function and method notation. These interchangeable forms allow flexibility in how you structure expressions.

extractTime(timestamp: timestamp, unit: dateunit | timeunit, tz: string?): number
(timestamp: timestamp).extractTime(unit: dateunit | timeunit, tz: string?): number

Arguments

NameTypeRequiredDescription
timestamptimestamptrueThe timestamp to be extracted
unitdateunit | timeunittrueMust be either a timeunit or a dateunit in long or short notation. (See below for examples)
tzstringfalseMust be a valid Time Zone string. See Time Zone section to find out more.

Valid unit inputs

The unit argument must be

  • any time unit in either long or short notation
  • a date unit in long notation: 'year''month''week''day_of_year''day_of_week'
  • a date unit in short notation: 'Y''M''W''doy''dow'

Example - Finding the hour in Tokyo

Consider the following timestamp:

1728636298

This timestamp refers to 11-10-2024T12:45:13Z. If we wish to find out just the hour in the Tokyo timezone, we can do all of this in a single command:

choose extractTime($m.timestamp, 'h', 'Asia/Tokyo') as h # Result 1: 8pm { "h": 20 }
choose $m.timestamp.extractTime('h', 'Asia/Tokyo') as h # Result 1: 8pm { "h": 20 }

Example - Extracting the number of seconds

Consider the following timestamp:

1728636298

This timestamp refers to 11-10-2024T12:45:13Z. If we wish to extract only the seconds portion here, we can use the second time unit:

choose extractTime(timestamp, 'second') as s # Result 2: 13.00 seconds { "s": 13.0 }
choose timestamp.extractTime('second') as s # Result 2: 13.00 seconds { "s": 13.0 }