Date Functions

Date Functions

N1QL date functions return the system clock value or manipulate the datetime values, which are represented as a string or an integer. These functions are very useful for manipulating dates in datasets with various date formats and timezones.

Timezones

Datetime values are always tied to a specific timezone, either explicitly in the date value, or implicitly in the application. The date functions in N1QL therefore support multiple different timezones.
UTC
UTC, The Coordinated Universal Time is the primary time standard by which the world regulates clocks and time. It is defined as the time at 0° longitude and is consistent, as it does not take into account daylight savings time. You can read further about UTC at https://www.timeanddate.com/time/aboututc.html.

All N1QL functions which accept a timezone as an argument also accept UTC.

IANA Timezones
Many applications operate across multiple different time zones and may not necessarily use UTC. Therefore, it is important for the database to be able to handle and manipulate dates in these time zones in a consistent manner. Many date functions take the time zone as an additional argument.
Note: Timezones are case sensitive, Europe/London is not the same as europe/london.

It is important to note that many time zones change their UTC offset based on daylight savings time, as a result the UTC offset of times may change based on the time of year. N1QL take this into account when converting dates.

Below are a few examples of commonly used timezones and their offsets:
Table 1. Common Timezones
Timezone UTC Offset (without daylight savings time) UTC Offset (during daylight savings time)
America/New_York -05:00 -04:00
America/Tijuana -08:00 -07:00
Europe/Paris +01:00 +02:00
Europe/London +00:00 +01:00
Asia/Tel_Aviv +02:00 +03:00
Asia/Kolkata +05:30 +05:30
Local System Timezone
Many functions default to using the local timezone of the system, which will be one of the IANA timezones.

Date Formats

N1QL date functions accept dates in either Epoch/UNIX timestamp format or string date format. N1QL is then able to represent the passed date as a standardized date object internally. In general, functions whose name contains the word STR are designed to use string formats while MILLIS functions are designed to use Epoch/UNIX timestamps.
Epoch/UNIX Timestamps
Epoch/UNIX time is the number of seconds (or milliseconds) that have elapsed since 1970-01-01T00:00:00.000Z (Thursday, 1 January 1970 at midnight), not including leap seconds. This can be useful for numeric and timezone agnostic representations of dates. While Epoch/UNIX time can be represented in either seconds or milliseconds, all N1QL date functions specifically treat Epoch/UNIX timestamps as milliseconds. For example, the date 2017-01-31T10:02:07Z would equate to an Epoch/UNIX timestamp of 1485856927000.
Date String Formats
In many cases, dates are not stored as Epoch/UNIX timestamp but instead as more human-readable formats, such as 2006-01-02T15:04:05.567+08:00. Therefore, N1QL also provides convenience methods to allow you to manipulate and convert dates in string format. All date formats follow the ISO-8601 standard.

The supported string formats are composed of the following components:

Table 2. Date String Components
Component Code Example
Year YYYY 2006
Month (of the year) MM 01
Day (of the month) DD 02
Hour (of the day) hh 15
Minute (of the hour) mm 04
Second (of the minute) ss 05
Millisecond (of the second) s 567
Time Zone (as UTC offset) TZD -08:00
Note: A UTC offset of 0 (+00:00) can just be specified as Z
The following table shows all of the accepted string date formats for N1QL date functions, using the date components specified above. In cases where the timezone is not specified, the local system time is assumed:
Table 3. Date String Examples
Format Example
YYYY-MM-DDThh:mm:ss.sTZD 2006-01-02T15:04:05.999+00:00 or 2006-01-02T15:04:05.999Z
YYYY-MM-DDThh:mm:ssTZD 2006-01-02T15:04:05+00:00 or 2006-01-02T15:04:05Z
YYYY-MM-DDThh:mm:ss.s 2006-01-02T15:04:05.999
YYYY-MM-DDThh:mm:ss 2006-01-02T15:04:05
YYYY-MM-DD hh:mm:ss.sTZD 2006-01-02 15:04:05.999+00:00 or 2006-01-02 15:04:05.999Z
YYYY-MM-DD hh:mm:ssTZD 2006-01-02 15:04:05+00:00 or 2006-01-02 15:04:05Z
YYYY-MM-DD hh:mm:ss.s 2006-01-02 15:04:05.999
YYYY-MM-DD hh:mm:ss 2006-01-02 15:04:05
YYYY-MM-DD 2006-01-02
hh:mm:ss.sTZD 15:04:05.999+00:00 or 15:04:05.999Z
hh:mm:ssTZD 15:04:05+00:00 or 15:04:05Z
hh:mm:ss.s 15:04:05.999
hh:mm:ss 15:04:05
Note: Currently N1QL only accepts numeric format strings with valid values for each component. For example, you should pass something like "1111-11-11" as the date format rather than "YYYY-MM-DD", this has the same effect as passing in any other valid date in the same format (such as "2017-01-31").

If the date string does not explicitly declare the value of a component, then a value of 0 is assumed. For example 2016-02-07 is equivalent to 2016-02-07T00:00:00. This is with the exception of the time zone, which if unspecified in the date string will default to the local system time zone.

N1QL is very specific as to what it accepts as a valid date format, below are a few examples:
  • "01:01:01" - Valid
  • "hh:mm:ss" - Invalid
  • "01:01:01.111" - Valid
  • "01:01.111" - Invalid
  • "2017-01-31" - Valid
  • "2017-01-86" - Invalid

Manipulating Date Components

Dates are composed of multiple different components such as the day, year, month etc. It is important for applications to be able to manipulate particular components of a date. Functions such as DATE_ADD_STR() accept a part argument, which is the component to adjust. Below is a list of accepted parts, these are expressed as strings and are not case-sensitive:
  • millennium

  • century

  • decade

  • year

  • quarter

  • month

  • week

  • day

  • hour

  • minute

  • second

  • millisecond

Extracting Date Components

It is important for applications to be able to extract the specific component of the timestamps, such as day, year, month, hours, minutes, or seconds, so that these can be used in N1QL queries. The following are the supported date parts that can be passed to the date extraction functions. These date parts are expressed as strings and are not case-sensitive, so year is regarded the same as YeAr. For all examples, the date being used is 2006-01-02T15:04:05.999Z

Table 4. Timestamp Components
Component Description Lower Bound Upper Bound Example
millennium The millennium (1000 year period), which begins at 0 BCE. - - 3
century The century (100 year period), which begins at 0 BCE. - - 21
decade The decade (10 year period). This is calculated as floor(year / 10). - - 200
year The Gregorian year, which begins at 0 BCE - - 2006
iso_year The ISO-8601 week number of the year. Each ISO-8601 year begins with the Monday of the week containing the 4th of January, so in early January and late December the ISO year may differ from the Gregorian year. Should be used in conjunction with iso_week to get consistent results. - - 2006
quarter The number of the quarter (3 month period) of the year. January-March (inclusive) is 1 while October-December (inclusive) is 4. 1 4 1
month The number of the month of the year. January is 1 and December is 12. 1 12 1
week The number of the week of the year. This is the ceiling value of the day of the year divided by 7. 1 53 1
iso_week The number of the week of the year, based on the ISO definition. ISO weeks start on Mondays and the first week of a year contains January 4 of that year. In other words, the first Thursday of a year will always be in week 1 of that year. This results in some different results between week and iso_week, based on the input date.

For example the iso_week of 2006-01-08T15:04:05.999Z is 1, while the week is 2. Should be used in conjunction with iso_year to get consistent results.

1 53 1
day The day of the month. 1 31 2
day_of_year or doy The day of the year. 1 366 2
day_of_week or dow The day of the week. 0 6 1
hour The hour of the day. 0 23 5
minute The minute of the hour. 0 59 4
second The second of the minute. 0 59 5
millisecond The millisecond of the second. 0 999 999
timezone The offset from UTC in seconds. -43200 43200 0
timezone_hour The hour component of the offset from UTC. -12 12 0
timezone_minute The minute component of the offset from UTC. -59 59 0

Date Functions

Below is a list of all date functions that N1QL provides.

Important: Many date functions use the local system clock value and timezone of the query node to generate results. Therefore, if all nodes running the query service do not have their time appropriately synchronized then you may experience inconsistent behavior. It is recommended that all Couchbase Server nodes have their time synchronized via NTP.
Note: If any arguments to any of the following functions are MISSING then the result is also MISSING (i.e. no result is returned). Similarly, if any of the arguments are NULL then NULL is returned.

CLOCK_LOCAL([fmt])

Description
The current time (at function evaluation time) of the machine that the query service is running on, in the specified string format.
Arguments
fmt
A string, or any valid expression which evaluates to a string, representing a supported date format to output the result as.

Optional argument, if no format or an incorrect format is specified, then this defaults to the combined full date and time.

Return Value
A date string in the format specified representing the local system time.
Limitations
CLOCK_LOCAL() cannot be used as part of an index definition, this includes the indexed fields and the WHERE clause of the index.
If this function is called multiple times within the same query then the values returned may differ, particularly if the query takes a long time to run. To avoid inconsistencies between multiple calls to CLOCK_LOCAL() within a single query, use NOW_LOCAL() instead.
Examples
SELECT CLOCK_LOCAL() as full_date, 
       CLOCK_LOCAL('invalid date') as invalid_date, 
       CLOCK_LOCAL('1111-11-11') as short_date;

{
    "results": [
        {
            "full_date": "2017-01-31T03:49:30.612-08:00",
            "invalid_date": "2017-01-31T03:49:30.612-08:00",
            "short_date": "2017-01-31"
        }
    ]
}

CLOCK_MILLIS()

Description
The current time as an Epoch/UNIX timestamp. Its fractional part represents nanoseconds, but the additional precision beyond milliseconds may not be consistent or guaranteed on all platforms.
Arguments
This function accepts no arguments.
Return Value
A single float value (with 3 decimal places) representing the system time as Epoch/UNIX time.
Limitations
CLOCK_MILLIS() cannot be used as part of an index definition, this includes the indexed fields and the WHERE clause of the index.
If this function is called multiple times within the same query then the values returned may differ, particularly if the query takes a long time to run. To avoid inconsistencies between multiple calls to CLOCK_MILLIS() within a single query, use NOW_MILLIS() instead.
Examples
SELECT CLOCK_MILLIS();
                            
{
    "results": [
        {
            "$1": 1483543192439.568
        }
    ]
}

CLOCK_STR([fmt])

Description
The current time (at function evaluation time) of the machine that the query service is running on, in the specified string format.
Arguments
fmt
A string, or any valid expression which evaluates to a string, representing a supported date format to output the result as. .
Optional argument, if no format or an incorrect format is specified, then this defaults to the combined full date and time.
Return Value
A date string in the format specified representing the system time.
Limitations
CLOCK_STR() cannot be used as part of an index definition, this includes the indexed fields and the WHERE clause of the index.
If this function is called multiple times within the same query then the values returned may differ, particularly if the query takes a long time to run. To avoid inconsistencies between multiple calls to CLOCK_STR() within a single query, use NOW_STR() instead.
Examples
SELECT CLOCK_STR() as full_date, 
       CLOCK_STR('invalid date') as invalid_date,
       CLOCK_STR('1111-11-11') as short_date;
    
{
    "results": [
        {
            "full_date": "2017-01-31T03:53:52.348-08:00",
            "invalid_date": "2017-01-31T03:53:52.348-08:00",
            "short_date": "2017-01-31"
        }
    ]
}

CLOCK_TZ(tz [, fmt])

Description
The current time (at function evaluation time) in the timezone given by the timezone argument passed to the function. This time is the local system time converted to the specified timezone.
Arguments
tz
A string, or any valid expression which evaluates to a string, representing the timezone to convert the local time to.
If this argument is not a valid timezone then null is returned as the result.
fmt
A string, or any valid expression which evaluates to a string, representing a supported date format to output the result as.
Optional argument, if no format or an incorrect format is specified, then this defaults to the combined full date and time.
Return Value
An date string in the format specified representing the system time in the specified timezone.
Limitations
As this function converts the local time, it may not accurately represent the true time in that timezone.
CLOCK_TZ() cannot be used as part of an index definition, this includes the indexed fields and the WHERE clause of the index.
If this function is called multiple times within the same query then the values returned may differ, particularly if the query takes a long time to run. To avoid inconsistencies between multiple calls to CLOCK_TZ() within a single query, use NOW_TZ() instead.
Examples
SELECT CLOCK_TZ('UTC') as UTC_full_date, 
       CLOCK_TZ('UTC', '1111-11-11') as UTC_short_date, 
       CLOCK_TZ('invalid timezone') as invalid_timezone, 
       CLOCK_TZ('US/Eastern') as us_east, 
       CLOCK_TZ('US/Pacific') as us_west;
	                    
{
    "results": [
        {
            "UTC_full_date": "2017-01-31T12:31:44.133Z",
            "UTC_short_date": "2017-01-31",
            "invalid_timezone": null,
            "us_east": "2017-01-31T07:31:44.133-05:00",
            "us_west": "2017-01-31T04:31:44.133-08:00"
        }
    ]
}

CLOCK_UTC([fmt])

Description
The current time in UTC. This time is the local system time converted to UTC. This function is provided for convenience and is the same as CLOCK_TZ('UTC').
Arguments
fmt
A string, or any valid expression which evaluates to a string, representing a supported date format to output the result as.
Optional argument, if no format or an incorrect format is specified, then this defaults to the combined full date and time.
Return Value
An date string in the format specified representing the system time in UTC.
Limitations
As this function converts the local time, it may not accurately represent the true time in UTC.
CLOCK_UTC() cannot be used as part of an index definition, this includes the indexed fields and the WHERE clause of the index.
If this function is called multiple times within the same query then the values returned may differ, particularly if the query takes a long time to run. To avoid inconsistencies between multiple calls to CLOCK_UTC() within a single query, use NOW_UTC() instead.
Examples
SELECT CLOCK_UTC() as full_date, CLOCK_UTC('1111-11-11') as short_date;

{
    "results": [
        {
            "full_date": "2017-01-31T14:13:03.147Z",
            "short_date": "2017-01-31"
        }
    ]
}

DATE_ADD_MILLIS(date1, n, part)

Description
Performs date arithmetic on a particular component of an Epoch/UNIX timestamp value. This calculation is specified by the arguments n and part.
For example, a value of 3 for n and a value of day for part would add 3 days to the date specified by date1.
Arguments
date1
An integer, or any valid expression which evaluates to an integer, representing an Epoch/UNIX timestamp in milliseconds.
If this argument is not an integer then null is returned.
n
The value to increment the date component by. This value must be an integer, or any valid expression which evaluates to an integer, and may be negative to perform date subtraction.
If a non-integer is passed to the function then null is returned.
part
A string, or any valid expression which evaluates to a string, representing the component of the date to increment.
If an invalid part is passed to the function then null is returned.
Return Value
An integer, representing the result of the calculation as an Epoch/UNIX timestamp in milliseconds.
Examples
SELECT DATE_ADD_MILLIS(1463284740000, 3, 'day') as add_3_days, 
       DATE_ADD_MILLIS(1463284740000, 3, 'year') as add_3_years,
       DATE_ADD_MILLIS(1463284740000, -3, 'day') as sub_3_days, 
       DATE_ADD_MILLIS(1463284740000, -3, 'year') as sub_3_years;
                        
{
    "results": [
        {
            "add_3_days": 1463543940000,
            "add_3_years": 1557892740000,
            "sub_3_days": 1463025540000,
            "sub_3_years": 1368590340000
        }
    ]
}

DATE_ADD_STR(date1, n, part)

Description
Performs date arithmetic on a date string. This calculation is specified by the arguments n and part. For example a value of 3 for n and a value of day for part would add 3 days to the date specified by date1.
Arguments
date1
A string, or any valid expression which evaluates to a string, representing the date in a supported date format.
n
The value to increment the date component by. This value must be an integer, or any valid expression which evaluates to an integer, and may be negative to perform date subtraction.
If a non-integer is passed to the function then null is returned.
part
A string, or any valid expression which evaluates to a string, representing the component of the date to increment.
If an invalid part is passed to the function then null is returned.
Return Value
An integer representing the result of the calculation as an Epoch/UNIX timestamp in milliseconds.
Examples
SELECT DATE_ADD_STR('2016-05-15 03:59:00Z', 3, 'day') as add_3_days,
       DATE_ADD_STR('2016-05-15 03:59:00Z', 3, 'year') as add_3_years,
       DATE_ADD_STR('2016-05-15 03:59:00Z', -3, 'day') as sub_3_days,
       DATE_ADD_STR('2016-05-15 03:59:00Z', -3, 'year') as sub_3_years;

{
    "results": [
        {
            "add_3_days": "2016-05-18T03:59:00Z",
            "add_3_years": "2019-05-15T03:59:00Z",
            "sub_3_days": "2016-05-12T03:59:00Z",
            "sub_3_years": "2013-05-15T03:59:00Z"
        }
    ]
}

DATE_DIFF_MILLIS(date1, date2, part)

Description
Finds the elapsed time between two Epoch/UNIX timestamps. This elapsed time is measured from the date specified by date2 to the date specified by date1. If date1 is greater than date2, then the value returned will be positive, otherwise the value returned will be negative.
Arguments
date1
An integer, or any valid expression which evaluates to an integer, representing a Epoch/UNIX timestamp in milliseconds. This is the value that is subtracted from date1.
If this argument is not an integer, then null is returned.
date2
An integer, or any valid expression which evaluates to an integer, representing a Epoch/UNIX timestamp in milliseconds.
This is the value that is subtracted from date1.
If this argument is not an integer, then null is returned.
part
A string, or any valid expression which evaluates to a string, representing the component of the date to increment.
If an invalid part is passed to the function, then null is returned.
Return Value
An integer representing the elapsed time (based on the specified part) between both dates.
Examples
SELECT DATE_DIFF_MILLIS(1463543940000, 1463284740000, 'day') as add_3_days,
       DATE_DIFF_MILLIS(1557892740000, 1463284740000, 'year') as add_3_years,
       DATE_DIFF_MILLIS(1463025540000, 1463284740000, 'day') as sub_3_days,
       DATE_DIFF_MILLIS(1368590340000, 1463284740000, 'year') as sub_3_years;

{
    "results": [
        {
            "add_3_days": 3,
            "add_3_years": 3,
            "sub_3_days": -3,
            "sub_3_years": -3
        }
    ]
}

DATE_DIFF_STR(date1, date2, part)

Description
Finds the elapsed time between two dates specified as formatted strings. This elapsed time is measured from the date specified by date2 to the date specified by date1. If date1 is greater than date2 then the value returned will be positive, otherwise the value returned will be negative.
Arguments
date1
An integer, or any valid expression which evaluates to an integer, representing a Epoch/UNIX timestamp in milliseconds. This is the value that is subtracted from date1.
If this argument is not an integer, then null is returned.
date2
An integer, or any valid expression which evaluates to an integer, representing a Epoch/UNIX timestamp in milliseconds.
This is the value that is subtracted from date1.
If this argument is not an integer, then null is returned.
part
A string, or any valid expression which evaluates to a string, representing the component of the date to increment.
If an invalid part is passed to the function, then null is returned.
Return Value
An integer representing the elapsed time (based on the specified part) between both dates.
Examples
SELECT DATE_DIFF_STR('2016-05-18T03:59:00Z', '2016-05-15 03:59:00Z', 'day') as add_3_days,
       DATE_DIFF_STR('2019-05-15T03:59:00Z', '2016-05-15 03:59:00Z', 'year') as add_3_years,
       DATE_DIFF_STR('2016-05-12T03:59:00Z', '2016-05-15 03:59:00Z', 'day') as sub_3_days,
       DATE_DIFF_STR('2013-05-15T03:59:00Z', '2016-05-15 03:59:00Z', 'year') as sub_3_years;
                        
{
    "results": [
        {
            "add_3_days": 3,
            "add_3_years": 3,
            "sub_3_days": -3,
            "sub_3_years": -3
        }
    ]
}

DATE_FORMAT_STR(date1, fmt)

Description
Converts datetime strings from one supported date string format to a different supported date string format.
Arguments
date1
A string, or any valid expression which evaluates to a string, representing a date in a supported date format.
If this argument is not a valid date string then null is returned.
fmt
A string, or any valid expression which evaluates to a string, representing a supported date format to output the result as.
If an incorrect format is specified then this defaults to the combined full date and time.
Return Value
A date string in the format specified.
Examples
SELECT DATE_FORMAT_STR('2016-05-15T00:00:23+00:00', '1111-11-11') as full_to_short,
       DATE_FORMAT_STR('2016-05-15', '1111-11-11T00:00:00+00:00') as short_to_full,
       DATE_FORMAT_STR('01:10:05', '1111-11-11T01:01:01Z') as time_to_full;

{
    "results": [
        {
            "full_to_short": "2016-05-15",
            "short_to_full": "2016-05-15T00:00:00-07:00",
            "time_to_full": "0000-01-01T01:10:05-08:00"
        }
    ]
}

DATE_PART_MILLIS(date1, part [, tz])

Description
Extracts the value of a given date component from an Epoch/UNIX timestamp value.
Arguments
date1
An integer, or any valid expression which evaluates to an integer, representing a Epoch/UNIX timestamp in milliseconds. This is the value that is subtracted from date1.
If this argument is not an integer, then null is returned.
part
A string, or any valid expression which evaluates to a string, representing the component of the date to increment.
If an invalid part is passed to the function, then null is returned.
tz
A string, or any valid expression which evaluates to a string, representing the timezone to convert the local time to.
Optional argument, defaults to the system timezone if not specified. If an incorrect time zone is provided, then null is returned.
Return Value
An integer representing the value of the component extracted from the timestamp.
Examples
SELECT DATE_PART_MILLIS(1463284740000, 'day') as day_local,
       DATE_PART_MILLIS(1463284740000, 'day', 'America/Tijuana') as day_pst,
       DATE_PART_MILLIS(1463284740000, 'day', 'UTC') as day_utc,
       DATE_PART_MILLIS(1463284740000, 'month') as month,
       DATE_PART_MILLIS(1463284740000, 'week') as week,
       DATE_PART_MILLIS(1463284740000, 'year') as year;

{
    "results": [
        {
            "day_local": 14,
            "day_pst": 14,
            "day_utc": 15,
            "month": 5,
            "week": 20,
            "year": 2016
        }
    ]
}                

DATE_PART_STR(date1, part)

Description
Extracts the value of a given date component from a date string.
Arguments
date1
An integer, or any valid expression which evaluates to an integer, representing a Epoch/UNIX timestamp in milliseconds. This is the value that is subtracted from date1.
If this argument is not an integer, then null is returned.
part
A string, or any valid expression which evaluates to a string, representing the component of the date to increment.
If an invalid part is passed to the function, then null is returned.
Return Value
An integer representing the value of the component extracted from the timestamp.
Examples
SELECT DATE_PART_STR('2016-05-15T03:59:00Z', 'day') as day,
       DATE_PART_STR('2016-05-15T03:59:00Z', 'millisecond') as millisecond,
       DATE_PART_STR('2016-05-15T03:59:00Z', 'month') as month,
       DATE_PART_STR('2016-05-15T03:59:00Z', 'week') as week,
       DATE_PART_STR('2016-05-15T03:59:00Z', 'year') as year;

{
    "results": [
        {
            "day": 15,
            "millisecond": 0,
            "month": 5,
            "week": 20,
            "year": 2016
        }
    ]
}           

DATE_RANGE_MILLIS(date1, date2, part [,n])

Description
Generates an array of dates from the start date specified by date1 and the end date specified by date2, as Epoch/UNIX timestamps. The difference between each subsequent generated date can be adjusted.
Arguments
date1
An integer, or any valid expression which evaluates to an integer, representing a Epoch/UNIX timestamp in milliseconds. This is the value that is subtracted from date1.
If this argument is not an integer, then null is returned.
date2
An integer, or any valid expression which evaluates to an integer, representing a Epoch/UNIX timestamp in milliseconds.
This is the value that is subtracted from date1.
If this argument is not an integer, then null is returned.
part
A string, or any valid expression which evaluates to a string, representing the component of the date to increment.
If an invalid part is passed to the function, then null is returned.
n
An integer, or any valid expression which evaluates to an integer, representing the value by which to increment the part component for each generated date.
Optional argument, if not specified, this defaults to 1. If a value which is not an integer is specified, then null is returned.
Return Value
An array of integers representing the generated dates, as Epoch/UNIX timestamps, between date1 and date2.
Limitations
It is possible to generate very large arrays using this function. In some cases the query engine may be unable to process all of these and cause excessive resource consumption. It is therefore recommended that you first validate the inputs to this function to ensure that the generated result is a reasonable size.
If the start date is greater than the end date passed to the function then an error will not be thrown, but the result array will be empty. An array of descending dates can be generated by setting the start date greater than the end date and specifying a negative value for n.
Examples
SELECT DATE_RANGE_MILLIS(1480752000000, 1475478000000, 'month', -1);
{
    "results": [
        {
            "$1": [
            1480752000000,
            1478156400000,
            1475478000000
            ]
        }
    ]
}

SELECT ARRAY_MILLIS_TO_STR(a,'2016-12-03') 
FOR a 
IN DATE_RANGE_MILLIS(1480752000000, 1449129600000, 'month', -1);

{
    "results": [
        {
            "$1": [
                "2016-12-03",
                "2016-11-03",
                "2016-10-03"            
            ]
        }
    ]
}

DATE_RANGE_STR(start_date, end_date, date_interval [, quantity_int ])

Description
Generates an array of date strings between the start date and end date, calculated by the interval and quantity values. The input dates can be in any of the supported date formats.
Arguments
start_date
A string, or any valid expression which evaluates to a string, representing a date in a supported date format. This is the date used as the start date of the array generation.
If this argument is not an integer, then null is returned.
end_date
A string, or any valid expression which evaluates to a string, representing a date in a supported date format. This is the date used as the end date of the array generation, and this value is exclusive, that is, the end date will not be included in the result.

If this argument is not an integer, then null is returned.

date_interval
A string, or any valid expression which evaluates to a string, representing the component of the date to increment.
If an invalid part is passed to the function, then null is returned.
quantity_int
An integer, or any valid expression which evaluates to an integer, representing the value by which to increment the interval component for each generated date.
Optional argument, if not specified, this defaults to 1. If a value which is not an integer is specified, then null is returned.
Return Value
An array of strings representing the generated dates, as date strings, between start_date and end_date.
Limitations
  • It is possible to generate very large arrays using this function. In some cases the query engine may be unable to process all of these and cause excessive resource consumption. It is therefore recommended that you first validate the inputs of this function to ensure that the generated result is a reasonable size.
  • If the start_date is greater than the end_date, then an error will not be thrown, but the result array will be empty. An array of descending dates can be generated by setting the start_date greater than the end_date and specifying a negative value for quantity_number.
  • From 4.6.2, both specified dates can be different acceptable date formats; but prior to 4.6.2, both specified dates must have the same string format, otherwise null will be returned. To ensure that both dates have the same format, you should use DATE_FORMAT_STR().
Examples
SELECT DATE_RANGE_STR('2015-11-30T15:04:05.999', '2017-04-14T15:04:06.998', 'quarter');  

[
    {
        "$1": [
            "2015-11-30T15:04:05.999",
            "2016-03-01T15:04:05.999",
            "2016-06-01T15:04:05.999",
            "2016-09-01T15:04:05.999",
            "2016-12-01T15:04:05.999",
            "2017-03-01T15:04:05.999"
        ]
    }
]

SELECT DATE_RANGE_STR('1111-11-11T15:04:05.999', '2016-01-05T15:04:05.998', 'day', 1);

[
    {
        "$1": [
            "1111-11-11T15:04:05.999",
            "2016-01-02T15:04:05.999",
            "2016-01-03T15:04:05.999",
            "2016-01-04T15:04:05.999"
        ]
    }
]

SELECT DATE_RANGE_STR('2018-01-01','2019-01-01', 'month', 4);

[
    {
        "$1": [
            "2018-01-01",
            "2018-05-01",
            "2018-09-01"
        ]
    }
]


SELECT DATE_RANGE_STR('2016-01-05T15:04:05.999', '1111-11-11T15:04:06.998', 'day', -1)"; 

{
    "results": [
        {
            "$1": [
                "2016-01-05T15:04:05.999",
                "2016-01-04T15:04:05.999",
                "2016-01-03T15:04:05.999",
                "2016-01-02T15:04:05.999"
            ]
        }
    ]
}
                        
SELECT DATE_RANGE_STR('2015-01-01T01:01:01', 
       DATE_FORMAT_STR('1111-11-11', '1111-11-11T00:00:00'), 'month');

{
    "results": [
        {
            "$1": [
                "2015-01-01T01:01:01",
                "2015-02-01T01:01:01",
                "2015-03-01T01:01:01",
                "2015-04-01T01:01:01",
                "2015-05-01T01:01:01",
                "2015-06-01T01:01:01",
                "2015-07-01T01:01:01",
                "2015-08-01T01:01:01",
                "2015-09-01T01:01:01",
                "2015-10-01T01:01:01",
                "2015-11-01T01:01:01",
                "2015-12-01T01:01:01"
            ]
        }
    ]
}

DATE_TRUNC_MILLIS(date1, part)

Description
Truncates an Epoch/UNIX timestamp up to the specified date component.
Arguments
date1
An integer, or any valid expression which evaluates to an integer, representing a Epoch/UNIX timestamp in milliseconds. This is the date used as the date to truncate.

If this argument is not an integer, then null is returned.

part
A string, or any valid expression which evaluates to a string, representing the component to truncate to.

If an invalid part is specified, then null is returned.

Return Value
An integer representing the truncated timestamp in Epoch/UNIX time.
Limitations
In some cases, where the timestamp is smaller than the duration of the provided part, this function returns the incorrect result. It is recommended that you do not use this function for very small Epoch/UNIX timestamps.
Examples
SELECT DATE_TRUNC_MILLIS(1463284740000, 'day') as day,
       DATE_TRUNC_MILLIS(1463284740000, 'month') as month,
       DATE_TRUNC_MILLIS(1463284740000, 'year') as year;

{
    "results": [
        {
            "day": 1463270400000,
            "month": 1462147200000,
            "year": 1451696400000
        }
    ]
}

DATE_TRUNC_STR(date1, part)

Description
Truncates a date string up to the specified date component.
Arguments
date1
A string, or any valid expression which evaluates to a string, representing a date in a supported date format. This is the date that is truncated.
If this argument is not a valid date format, then null is returned.
part
A string, or any valid expression which evaluates to a string, representing the component to truncate to.
If an invalid part is specified, then null is returned.
Return Value
A date string representing the truncated date.
Examples
SELECT DATE_TRUNC_STR('2016-05-18T03:59:00Z', 'day') as day,
       DATE_TRUNC_STR('2016-05-18T03:59:00Z', 'month') as month,
       DATE_TRUNC_STR('2016-05-18T03:59:00Z', 'year') as year;

{
    "results": [
        {
            "day": "2016-05-18T00:00:00Z",
            "month": "2016-05-01T00:00:00Z",
            "year": "1111-11-11T00:00:00Z"
        }
    ]
}

DURATION_TO_STR(duration)

Description
Converts a number into a human-readable time duration with units.
Arguments
duration
A number, or any valid expression which evaluates to a number, which represents the duration to convert to a string. This value is specified in nanoseconds ( 1x10-9 seconds).
If a value which is not a number is specified, then null is returned.
Return Value
A string representing the human-readable duration.
Examples
SELECT DURATION_TO_STR(2000) as microsecs,
       DURATION_TO_STR(2000000) as millisecs,
       DURATION_TO_STR(2000000000) as secs;
                            
{
    "results": [
        {
            "microsecs": "2µs",
            "millisecs": "2ms",
            "secs": "2s"
        }
    ]
}

MILLIS(date1)

Description
Converts a date string to Epoch/UNIX milliseconds.
Arguments
date1
A string, or any valid expression which evaluates to a string, representing a date in a supported date format. This is the date to convert to Epoch/UNIX milliseconds.
If this argument is not a valid date format. then null is returned.
Return Value
An integer representing the date string converted to Epoch/UNIX milliseconds.
Examples
SELECT MILLIS("2016-05-15T03:59:00Z");
{
    "results": [
        {
        "$1": 1463284740000
        }
    ]
}

MILLIS_TO_LOCAL(date1 [, fmt])

Alias for MILLIS_TO_STR().

MILLIS_TO_STR(date1 [, fmt ])

Description
Converts an Epoch/UNIX timestamp into the specified date string format.
Arguments
date1
An integer, or any valid expression which evaluates to an integer, representing a Epoch/UNIX timestamp in milliseconds. This is the date to convert.
If this argument is not an integer, then null is returned.
fmt
A string, or any valid expression which evaluates to a string, representing a supported date format to output the result as.
Optional argument, if unspecified or an incorrect format is specified, then this defaults to the combined full date and time.
Return Value
A date string representing the local date in the specified format.
Limitations
In some cases, where the timestamp is smaller than the duration of the provided part, this function returns the incorrect result. It is recommended that you do not use this function for very small Epoch/UNIX timestamps.
Examples
SELECT MILLIS_TO_STR(1463284740000) as full_date, 
       MILLIS_TO_STR(1463284740000, 'invalid format') as invalid_format,
       MILLIS_TO_STR(1463284740000, '1111-11-11') as short_date;

{
    "results": [
        {
            "full_date": "2016-05-14T20:59:00-07:00",
            "invalid_format": "2016-05-14T20:59:00-07:00",
            "short_date": "2016-05-14"
        }
    ]
}

MILLIS_TO_TZ(date1, tz [, fmt])

Description
Converts an Epoch/UNIX timestamp into the specified time zone in the specified date string format.
Arguments
date1
An integer, or any valid expression which evaluates to an integer, representing a Epoch/UNIX timestamp in milliseconds. This is the date to convert.
If this argument is not an integer, then null is returned.
tz
A string, or any valid expression which evaluates to a string, representing the timezone to convert the local time to. Optional argument, defaults to the system timezone if not specified.
If an incorrect time zone is provided, then null is returned.
fmt
A string, or any valid expression which evaluates to a string, representing a supported date format to output the result as.
Optional argument, if no format or an incorrect format is specified, then this defaults to the combined full date and time.
Return Value
A date string representing the date in the specified timezone in the specified format..
Examples
SELECT MILLIS_TO_TZ(1463284740000, 'America/New_York') as est,
	   MILLIS_TO_TZ(1463284740000, 'Asia/Kolkata') as ist,
	   MILLIS_TO_TZ(1463284740000, 'UTC') as utc;
	                        
{
    "results": [
	    {
	        "est": "2016-05-14T23:59:00-04:00",
	        "invalid_tz": "2016-05-15T03:59:00Z",
	        "ist": "2016-05-15T09:29:00+05:30",
	        "utc": "2016-05-15T03:59:00Z"
	    }
	]
}

MILLIS_TO_UTC(date1 [, fmt])

Description
Converts an Epoch/UNIX timestamp into local time in the specified date string format.
Arguments
date1
An integer, or any valid expression which evaluates to an integer, representing a Epoch/UNIX timestamp in milliseconds. This is the date to convert to UTC.
If this argument is not an integer, then null is returned.
fmt
A string, or any valid expression which evaluates to a string, representing a supported date format to output the result as.
Optional argument, if unspecified or an incorrect format is specified, then this defaults to the combined full date and time.
Return Value
A date string representing the date in UTC in the specified format.
Examples
SELECT MILLIS_TO_UTC(1463284740000) as full_date, 
       MILLIS_TO_UTC(1463284740000, 'invalid format') as invalid_format,
       MILLIS_TO_UTC(1463284740000, '1111-11-11') as short_date;

{
    "results": [
        {
            "full_date": "2016-05-15T03:59:00Z",
            "invalid_format": "2016-05-15T03:59:00Z",
            "short_date": "2016-05-15"
        }
    ]
}

MILLIS_TO_ZONE_NAME(date1, tz [, fmt])

Alias for MILLIS_TO_TZ()

NOW_LOCAL([fmt])

Description
The timestamp of the query as date string in the system timezone. Will not vary during a query.
Arguments
fmt
A string, or any valid expression which evaluates to a string, representing a supported date format to output the result as.
Optional argument, if no format or an incorrect format is specified, then this defaults to the combined full date and time.
Return Value
A date time string in the format specified.
Limitations
If this function is called multiple times within the same query it will always return the same time. If you wish to use the system time when the function is evaluated, use CLOCK_LOCAL() instead.
Examples
SELECT NOW_LOCAL() as full_date, 
       NOW_LOCAL('invalid date') as invalid_date,
       NOW_LOCAL('1111-11-11') as short_date;

{
    "results": [
        {
            "full_date": "2017-02-03T03:10:19.01-08:00",
            "invalid_date": "2017-02-03T03:10:19.01-08:00",
            "short_date": "2017-02-03"
        }
    ]
}

SELECT NOW_LOCAL(), NOW_LOCAL(), NOW_LOCAL(), NOW_LOCAL(),
       NOW_LOCAL(), NOW_LOCAL(), CLOCK_LOCAL();

{
    "results": [
        {
            "$1": "2017-02-02T06:56:09.67-08:00",
            "$2": "2017-02-02T06:56:09.67-08:00",
            "$3": "2017-02-02T06:56:09.67-08:00",
            "$4": "2017-02-02T06:56:09.67-08:00",
            "$5": "2017-02-02T06:56:09.67-08:00",
            "$6": "2017-02-02T06:56:09.67-08:00",
            "$7": "2017-02-02T06:56:09.673-08:00"
        }
    ]
}

NOW_MILLIS()

Description
The timestamp of the query as an Epoch/UNIX timestamp. Will not vary during a query.
Arguments
This function accepts no arguments.
Return Value
A floating point number representing the Epoch/UNIX timestamp of the query.
Limitations
If this function is called multiple times within the same query it will always return the same time. If you wish to use the system time when the function is evaluated, use CLOCK_MILLIS() instead.
Examples
SELECT NOW_MILLIS();
{
    "results": [
        {
            "$1": 1486046939117.3264
        }
    ]
}

SELECT NOW_MILLIS(), NOW_MILLIS(), NOW_MILLIS(), NOW_MILLIS(), 
       NOW_MILLIS(), NOW_MILLIS(), CLOCK_MILLIS();

{
    "results": [
        {
            "$1": 1486046983987.5046,
            "$2": 1486046983987.5046,
            "$3": 1486046983987.5046,
            "$4": 1486046983987.5046,
            "$5": 1486046983987.5046,
            "$6": 1486046983987.5046,
            "$7": 1486046983987.6514
        }
    ]
}

NOW_TZ(tz [, fmt])

Description
The timestamp of the query as date string in the specified timezone. Will not vary during a query.
Arguments
tz
A string, or any valid expression which evaluates to a string, representing the timezone to convert the query timestamp to.
If an incorrect time zone is provided then null is returned.
fmt
A string, or any valid expression which evaluates to a string, representing a supported date format to output the result as.
Optional argument, if unspecified or an incorrect format is specified, then this defaults to the combined full date and time.
Return Value
A date string in the format specified representing the timestamp of the query in the specified timezone.
Limitations
If this function is called multiple times within the same query it will always return the same time. If you wish to use the system time when the function is evaluated, use CLOCK_TZ() instead.
Examples
SELECT NOW_TZ('invalid tz') as invalid_tz,
       NOW_TZ('Asia/Kolkata') as ist,
       NOW_TZ('UTC') as utc,
       NOW_TZ('UTC', '1111-11-11') as utc_short_date;

{
    "results": [
        {
            "invalid_tz": null,
            "ist": "2017-02-03T16:42:42.686+05:30",
            "utc": "2017-02-03T11:12:42.686Z",
            "utc_short_date": "2017-02-03"
        }
    ]
}

SELECT NOW_TZ('UTC'), NOW_TZ('UTC'), NOW_TZ('UTC'), NOW_TZ('UTC'), 
       NOW_TZ('UTC'), NOW_TZ('UTC'), NOW_TZ('UTC'), CLOCK_TZ('UTC');

{
    "results": [
        {
            "$1": "2017-02-03T11:15:44.457Z",
            "$2": "2017-02-03T11:15:44.457Z",
            "$3": "2017-02-03T11:15:44.457Z",
            "$4": "2017-02-03T11:15:44.457Z",
            "$5": "2017-02-03T11:15:44.457Z",
            "$6": "2017-02-03T11:15:44.457Z",
            "$7": "2017-02-03T11:15:44.457Z",
            "$8": "2017-02-03T11:15:44.459Z"
        }
    ]
}

NOW_STR([fmt])

Description
The timestamp of the query as date string in the system timezone. Will not vary during a query.
Arguments
fmt
A string, or any valid expression which evaluates to a string, representing a supported date format to output the result as.
Optional argument, if unspecified or an incorrect format is specified, then this defaults to the combined full date and time.
Return Value
A date string in the format specified representing the timestamp of the query.
Limitations
If this function is called multiple times within the same query it will always return the same time. If you wish to use the system time when the function is evaluated, use CLOCK_STR() instead.
Examples
SELECT NOW_STR() as full_date,
       NOW_STR('invalid date') as invalid_date,
       NOW_STR('1111-11-11') as short_date;

{
    "results": [
        {
            "full_date": "2017-02-03T03:21:29.814-08:00",
            "invalid_date": "2017-02-03T03:21:29.814-08:00",
            "short_date": "2017-02-03"
        }
    ]
}
                            
SELECT NOW_STR(), NOW_STR(), NOW_STR(), NOW_STR(), NOW_STR(), NOW_STR(), CLOCK_STR();
                            
{
    "results": [
        {
            "$1": "2017-02-03T03:22:34.486-08:00",
            "$2": "2017-02-03T03:22:34.486-08:00",
            "$3": "2017-02-03T03:22:34.486-08:00",
            "$4": "2017-02-03T03:22:34.486-08:00",
            "$5": "2017-02-03T03:22:34.486-08:00",
            "$6": "2017-02-03T03:22:34.486-08:00",
            "$7": "2017-02-03T03:22:34.488-08:00"
        }
    ]
}

NOW_UTC([fmt])

Description
The timestamp of the query as date string in UTC. Will not vary during a query.
Arguments
fmt
A string, or any valid expression which evaluates to a string, representing a supported date format to output the result as.
Optional argument, if unspecified or an incorrect format is specified, then this defaults to the combined full date and time.
Return Value
A date string in the format specified representing the timestamp of the query in UTC.
Limitations
If this function is called multiple times within the same query it will always return the same time. If you wish to use the system time when the function is evaluated, use CLOCK_MILLIS() instead.
Examples
SELECT NOW_UTC();

{
    "results": [
        {
            "$1": "2017-02-03T11:16:54.419Z"
        }
    ]
}
                            
SELECT NOW_UTC(), NOW_UTC(), NOW_UTC(), NOW_UTC(), NOW_UTC(), NOW_UTC(), NOW_UTC(), CLOCK_UTC();
                            
{
    "results": [
        {
            "$1": "2017-02-03T11:18:27.183Z",
            "$2": "2017-02-03T11:18:27.183Z",
            "$3": "2017-02-03T11:18:27.183Z",
            "$4": "2017-02-03T11:18:27.183Z",
            "$5": "2017-02-03T11:18:27.183Z",
            "$6": "2017-02-03T11:18:27.183Z",
            "$7": "2017-02-03T11:18:27.183Z",
            "$8": "2017-02-03T11:18:27.185Z"
        }
    ]                            
}

STR_TO_DURATION(duration)

Description
Converts a string representation of a time duration into nanoseconds. This accepts the following units:
  • nanoseconds (ns)
  • microseconds (us or µs)
  • milliseconds (ms)
  • seconds (s)
  • minutes (m)
  • hours (h)
Arguments
duration
A string, or any valid expression which evaluates to a string, representing the duration to convert.
If an invalid duration string is specified, then null is returned.
Return Value
A single integer representing the duration in nanoseconds.
Examples
SELECT STR_TO_DURATION('1h') as hour,
STR_TO_DURATION('1us') as microsecond,
STR_TO_DURATION('1ms') as millisecond,
STR_TO_DURATION('1m') as minute,
STR_TO_DURATION('1ns') as nanosecond,
STR_TO_DURATION('1s') as second;

{
    "results": [
        {
            "hour": 3600000000000,
            "microsecond": 1000,
            "millisecond": 1000000,
            "minute": 60000000000,
            "nanosecond": 1,
            "second": 1000000000
        }
    ]
}

STR_TO_MILLIS(date1)

Description
Converts a date string to Epoch/UNIX milliseconds.
Arguments
date1
A string, or any valid expression which evaluates to a string, representing a date in a supported date format. This is the date to convert to Epoch/UNIX milliseconds.
If this argument is not a valid date format, then null is returned.
Return Value
An integer representing the date string converted to Epoch/UNIX milliseconds.
Examples
SELECT STR_TO_MILLIS("2016-05-15T03:59:00Z");

{
    "results": [
        {
        "$1": 1463284740000
        }
    ]
}

STR_TO_UTC(date1)

Description
Converts a date string into the equivalent date in UTC. The output date format follows the date format of the date passed as input.
Arguments
date1
A string, or any valid expression which evaluates to a string, representing a date in a supported date format. This is the date to convert to UTC.
If this argument is not a valid date format, then null is returned.
Return Value
A single date string representing the date string converted to UTC.
Examples
SELECT STR_TO_UTC('1111-11-11T00:00:00+08:00') as full_date,
STR_TO_UTC('1111-11-11') as short_date;
                            
{
    "results": [
        {
            "full_date": "2015-12-31T16:00:00Z",
            "short_date": "1111-11-11"
        }
    ]
}

STR_TO_TZ(date1, tz)

Description
Converts a date string to its equivalent in the specified timezone. The output date format follows the date format of the date passed as input.
Arguments
date1
A string, or any valid expression which evaluates to a string, representing a date in a supported date format. This is the date to convert to UTC.
If this argument is not a valid date format then null is returned.
tz
A string, or any valid expression which evaluates to a string, representing the timezone to convert the local time to.
If this argument is not a valid timezone, then null is returned.
Return Value
A single date string representing the date string converted to the specified timezone.
Examples
SELECT STR_TO_TZ('1111-11-11T00:00:00+08:00', 'America/New_York') as est,
    STR_TO_TZ('1111-11-11T00:00:00+08:00', 'UTC') as utc,
    STR_TO_TZ('1111-11-11', 'UTC') as utc_short;
                            
{
    "results": [
        {
            "est": "2015-12-31T11:00:00-05:00",
            "utc": "2015-12-31T16:00:00Z",
            "utc_short": "1111-11-11"
        }
    ]
}

STR_TO_ZONE_NAME(date1, tz)

Alias for STR_TO_TZ().

WEEKDAY_MILLIS(expr [, tz ])

Description
Converts a date string to its equivalent in the specified timezone. The output date format follows the date format of the date passed as input.
Arguments
expr
An integer, or any valid expression which evaluates to an integer, representing an Epoch/UNIX timestamp in milliseconds.
tz
A string, or any valid expression which evaluates to a string, representing the timezone to for the expr argument.
Optional argument, defaults to the system timezone if not specified. If an incorrect time zone is provided then null is returned.
Return Value
A single date string representing the date string converted to the specified timezone.
Examples
SELECT WEEKDAY_MILLIS(1486237655742, 'America/Tijuana');
                            
{
    "results": [
    {
        "$1": "Saturday"
    }
]

WEEKDAY_STR(date)

Description
Returns the day of the week string value from the input date string. Returns the weekday name from the input date in Unix timestamp. Note that his function returns the string value of the day of the week, where DATE_PART_STR() with part = "dow" returns an integer value of the weekday (0-6).
Arguments
date
A string, or any valid expression which evaluates to a string, representing a date in a supported date format. This is the date to convert to UTC.
If this argument is not a valid date format then null is returned.
Return Value
The text string name of the day of the week, such as "Monday" or "Friday".
Examples
SELECT WEEKDAY_STR('2017-02-05');
                            
[
    {
        "$1": "Sunday"
    }
]