Time Functions

CURRENT_TIME

CURRENT_TIME([timezone]) Returns current time in the timezone time zone (default UTC). Return value is of time type.

SELECT CURRENT_TIME()
18:19:06.705792
SELECT CURRENT_TIME('America/Los_Angeles')
11:19:06.705792
SELECT TYPEOF(CURRENT_TIME('America/Los_Angeles'))
'time'
SELECT CAST(CURRENT_TIME('America/Los_Angeles') AS string)
'11:19:06.705792'
SELECT FORMAT_TIME('%H:%M', CURRENT_TIME())
'18:19'

Here are some examples of comparison and interval arithmetic operations on the time type:

SELECT CURRENT_TIME() > PARSE_TIME('%H:%M', '10:10')
true
SELECT CURRENT_TIME() > CURRENT_TIME() - INTERVAL 1 SECOND
true
SELECT
    CURRENT_TIME() AS now,
    (CURRENT_TIME() - INTERVAL 1 HOUR) AS one_hour_ago
+---------------------+-------------------+ | now | one_hour_ago | |---------------------+-------------------+ | 18:19:06.705792 | 17:19:06.705792 | +---------------------+-------------------+

TIME constructor

TIME(hour, min, sec[, microsecond])

Constructs value of type time based on hour, min, sec, and, optionally, microsecond.

SELECT TIME(10,30,10)
10:30:10
SELECT TYPEOF(TIME(10,30,10))
'time'

Here are some examples of comparison and interval arithmetic operations on the time type:

SELECT TIME(10,30,10) > PARSE_TIME('%H.%M.%S', '10.30.10')
false
SELECT TIME(10,30,10) > PARSE_TIME('%H.%M.%S', '10.30.10') - INTERVAL 1 SECOND
true
SELECT
    TIME(10,31,10) AS now,
    (TIME(10,31,10) - INTERVAL 1 MINUTE) AS one_minute_ago
+---------------+----------------+ | now | one_minute_ago | |---------------+----------------+ | 10:31:10 | 10:30:10 | +---------------+----------------+
TIME(datetime)

Returns the time part of datetime.

SELECT CURRENT_DATETIME()
2019-08-19T18:19:06.705792
SELECT TIME(CURRENT_DATETIME())
18:19:06.705792
SELECT TYPEOF(TIME(CURRENT_DATETIME()))
'time'

Here are some examples of comparison and interval arithmetic operations on the time type:

SELECT TIME(CURRENT_DATETIME()) > TIME(11,10,10)
true
SELECT TIME(CURRENT_DATETIME()) > TIME(CURRENT_DATETIME()) - INTERVAL 1 SECOND
true
SELECT
    TIME(CURRENT_DATETIME()) AS now,
    (TIME(CURRENT_DATETIME()) - INTERVAL 1 MINUTE) AS one_minute_ago
+------------------+-----------------+ | now | one_minute_ago | |------------------+-----------------+ | 18:19:06.705792 | 18:18:06.705792 | +------------------+-----------------+

CURRENT_TIME([timezone]) Returns current time in the timezone time zone (default UTC). Return value is of time type.

SELECT CURRENT_TIMESTAMP()
'2019-08-19T18:19:06.705792Z'
SELECT TIME(CURRENT_TIMESTAMP())
18:19:06.705792
SELECT TYPEOF(TIME(CURRENT_TIMESTAMP()))
'time'

Here are some examples of comparison and interval arithmetic operations on the time type:

SELECT TIME(CURRENT_TIMESTAMP()) > TIME(11,10,10)
true
SELECT TIME(CURRENT_TIMESTAMP()) > TIME(CURRENT_TIMESTAMP()) - INTERVAL 1 SECOND
true
SELECT
    TIME(CURRENT_TIMESTAMP()) AS now,
    (TIME(CURRENT_TIMESTAMP()) - INTERVAL 1 MINUTE) AS one_minute_ago
+------------------+-----------------+ | now | one_minute_ago | |------------------+-----------------+ | 18:19:06.705792 | 18:18:06.705792 | +------------------+-----------------+

FORMAT_TIME

FORMAT_TIME(format, time) Converts time to string using the given format.

The format specification is the standard strftime with CCTZ extensions.

SELECT FORMAT_TIME('%H:%M:%E*S', TIME '21:30:20.345')
'21:30:20.345'
SELECT FORMAT_TIME('%I:%M:%S', TIME '21:30:20.345')
'09:30:20'

FORMAT_ISO8601 time

FORMAT_ISO8601(time)

Converts a time value to string using the ISO 8601 extended format. Returns string formatted using HH:MM:SS[.cccccc] as the format specifier for time. It formats it as a 24 hour time, with optional fractional seconds.

SELECT FORMAT_ISO8601(TIME '11:11:10.1002')
'11:11:10.100200'
SELECT FORMAT_ISO8601(TIME '21:11:10')
'21:11:10'

PARSE_TIME

PARSE_TIME(format, string) Parses the date string (formatted using the given format) into a time value.

The format specification is the standard strptime with CCTZ extensions.

SELECT PARSE_TIME('%H:%M:%S', '10:30:10')
10:30:10
SELECT TYPEOF(PARSE_TIME('%H:%M:%S', '10:30:10'))
'time'

Here are some examples of comparison and interval arithmetic operations on the time type:

SELECT TIME '10:31:00' > PARSE_TIME('%H.%M.%S', '10.30.10')
true
SELECT TIME(10,30,10) > PARSE_TIME('%H.%M.%S', '10.30.10') - INTERVAL 1 SECOND
true
SELECT
    PARSE_TIME('%H:%M:%S', '10:31:10') AS now,
    (PARSE_TIME('%H:%M:%S', '10:31:10') - INTERVAL 1 MINUTE) AS one_minute_ago
+---------------+----------------+ | now | one_minute_ago | |---------------+----------------+ | 10:31:10 | 10:30:10 | +---------------+----------------+

PARSE_TIME_ISO8601

PARSE_TIME_ISO8601(string) Parses a time from an ISO 8601 string without a timezone.

SELECT PARSE_TIME_ISO8601('10:30:10')
10:30:10
SELECT TYPEOF(PARSE_TIME_ISO8601('10:30:10'))
'time'

Here are some examples of comparison and interval arithmetic operations on the time type:

SELECT TIME(10,31,10) > PARSE_TIME_ISO8601('10:30:10')
true
SELECT TIME '10:30:10' > PARSE_TIME_ISO8601('10:30:10') - INTERVAL 1 SECOND
true
SELECT
    PARSE_TIME_ISO8601('10:31:10') AS now,
    (PARSE_TIME_ISO8601('10:31:10') - INTERVAL 1 MINUTE) AS one_minute_ago
+---------------+----------------+ | now | one_minute_ago | |---------------+----------------+ | 10:31:10 | 10:30:10 | +---------------+----------------+

EXTRACT time

EXTRACT(part FROM time)

Extracts a component specified by part from time.

Here are supported values for part for time along with example queries.

TypePartQueryResult
TIMEMICROSECONDSELECT EXTRACT(MICROSECOND FROM TIME '04:30:20.345')345000
TIMEMILLISECONDSELECT EXTRACT(MILLISECOND FROM TIME '04:30:20.345')345
TIMESECONDSELECT EXTRACT(SECOND FROM TIME '04:30:20.345')20
TIMEMINUTESELECT EXTRACT(MINUTE FROM TIME '04:30:20.345')30
TIMEHOURSELECT EXTRACT(HOUR FROM TIME '04:30:20.345')4
TIMEDAYSELECT EXTRACT(DAY FROM TIME '04:30:20.345')1
TIMEDAYOFWEEKSELECT EXTRACT(DAYOFWEEK FROM TIME '04:30:20.345')5
TIMEDOWSELECT EXTRACT(DOW FROM TIME '04:30:20.345')5
TIMEDAYOFYEARSELECT EXTRACT(DAYOFYEAR FROM TIME '04:30:20.345')1
TIMEDOYSELECT EXTRACT(DOY FROM TIME '04:30:20.345')1
TIMEMONTHSELECT EXTRACT(MONTH FROM TIME '04:30:20.345')1
TIMEQUARTERSELECT EXTRACT(QUARTER FROM TIME '04:30:20.345')1
TIMEYEARSELECT EXTRACT(YEAR FROM TIME '04:30:20.345')1970
TIMEDATESELECT EXTRACT(DATE FROM TIME '04:30:20.345')1970-01-01
TIMETIMESELECT EXTRACT(TIME FROM TIME '04:30:20.345')04:30:20.345
TIMEEPOCHSELECT EXTRACT(EPOCH FROM TIME '04:30:20.345')16220
TIMEISOWEEKSELECT EXTRACT(ISOWEEK FROM TIME '04:30:20.345')1
TIMEISOYEARSELECT EXTRACT(ISOYEAR FROM TIME '04:30:20.345')1970