Time functions

Updated at:

The following table describes the time functions.

Function

Feature

ADD_DAYS

Increases the day of a date by a specified amount.

ADD_MONTHS

Increases the month of a date by a specified amount.

ADD_YEARS

Increases the year of a date by a specified amount.

DATE_DIFF

Calculates the difference between two time values.

DATE_FORMAT

Formats a date into a string of a specified format.

FROM_UNIXTIME

Converts an integer timestamp in seconds to a date value.

GET_DAY_OF_MONTH

Returns the day part of a time value.

GET_HOUR

Gets the hour part of a time value.

GET_MINUTE

Gets the minute part of a time value.

GET_MONTH

Gets the month part of a time value.

GET_QUARTER

Gets the quarter corresponding to a time value.

GET_SECOND

Gets the second part of a date and time value.

GET_WEEKDAY

Gets the day of the week for a specified date.

GET_WEEK_OF_YEAR

Gets the week of the year for a specified date.

GET_YEAR

Gets the year part of a time value.

NOW

Gets the current system time.

SUB_DAYS

Decreases the day of a date by a specified amount.

TO_DATE

Converts a string of a specified format to a date and time value.

UNIX_TIMESTAMP

Converts a date and time value to an integer timestamp in seconds.

ADD_DAYS

  • Function description

Adds the number of days specified by delta to startdate.

  • Parameters

    • startdate: Required. A value of the time data type to which days will be added.

    • delta: Required. An integer that specifies the number of days to add. If delta is greater than 0, days are added. Otherwise, days are subtracted.

  • Return value

Returns the calculated date. The result is truncated to the `yyyy-MM-dd 00:00:00` format. If the startdate or delta parameter is NULL, this function returns NULL.

  • Supported compute engines

MaxCompute (ODPS), MYSQL, RDS, HIVE, AnalyticDB for PostgreSQL (ADB_PG), POLAR_PG, POSTGRESQL, and SPARK.

ADD_MONTHS

  • Function description

Adds the number of months specified by delta to startdate.

  • Parameters

    • startdate: Required. A value of the time data type to which months will be added.

    • delta: Required. An integer that specifies the number of months to add. If delta is greater than 0, months are added. Otherwise, months are subtracted.

  • Return value

Returns the calculated date. The result is truncated to the `yyyy-MM-dd 00:00:00` format. If the startdate or delta parameter is NULL, this function returns NULL.

  • Supported compute engines

MaxCompute (ODPS), MYSQL, RDS, HIVE, ADB_PG, POLAR_PG, POSTGRESQL, and SPARK.

ADD_YEARS

  • Function description

Adds the number of years specified by delta to startdate.

  • Parameters

    • startdate: Required. A value of the time data type to which years will be added.

    • delta: Required. An integer that specifies the number of years to add. If delta is greater than 0, years are added. Otherwise, years are subtracted.

  • Return value

Returns the calculated date. The result is truncated to the `yyyy-MM-dd 00:00:00` format. If the startdate or delta parameter is NULL, this function returns NULL.

  • Supported compute engines

MaxCompute (ODPS), MYSQL, RDS, HIVE, ADB_PG, POLAR_PG, POSTGRESQL, and SPARK.

DATE_DIFF

  • Function description

Calculates the difference between two time values, start and end, in the specified time unit dateUnit.

  • Parameters

    • start: Required. A value of the time data type that specifies the start time.

    • end: Required. A value of the time data type that specifies the end time.

    • dateUnit: Required. A string that specifies the time unit. Valid values are YEAR, MONTH, DAY, HOUR, MINUTE, and SECOND.

  • Return value

Returns the difference between the two time values as an integer. A negative number indicates that the start time is later than the end time. If the start or end parameter is NULL, this function returns NULL.

  • Supported compute engines

MaxCompute (ODPS), MYSQL, RDS, HIVE, ADB_PG, POLAR_PG, POSTGRESQL, and SPARK.

DATE_FORMAT

  • Function description

Formats a date value into a string based on a specified format.

  • Parameters

    • dateValue: Required. A value of the time data type.

    • format: Required. A string that specifies the date format. Valid values are yyyy-MM-dd HH:mm:ss, yyyy-MM-dd, yyyyMMdd, and yyyyMMddHHmmss.

  • Return value

Returns the formatted string. If the dateValue parameter is NULL, this function returns NULL.

  • Supported compute engines

MaxCompute (ODPS), MYSQL, RDS, HIVE, ADB_PG, POLAR_PG, POSTGRESQL, and SPARK.

FROM_UNIXTIME

  • Function description

Converts an integer timestamp in seconds to a date value.

  • Parameters

    • unixtime: Required. An integer that specifies the timestamp in seconds.

  • Return value

Returns the converted date and time value. Returns NULL if the unixtime parameter is NULL.

  • Supported compute engines

MaxCompute (ODPS), MySQL, RDS, Hive, ADB_PG, POLAR_PG, PostgreSQL, and Spark.

GET_DAY_OF_MONTH

  • Function description

Returns the day of the month from a time value.

  • Parameters

    • dateValue: Required. A value of the time data type.

  • Return value

Returns an integer from 1 to 31 that represents the day of the month. If the dateValue parameter is NULL, this function returns NULL.

  • Supported compute engines

MaxCompute (ODPS), MYSQL, RDS, HIVE, ADB_PG, POLAR_PG, POSTGRESQL, and SPARK.

GET_HOUR

  • Function description

Returns the hour part of a time value.

  • Parameters

    • dateValue: Required. A value of the time data type.

  • Return value

Returns an integer from 0 to 23 that represents the hour. If the dateValue parameter is NULL, this function returns NULL.

  • Supported compute engines

MaxCompute (ODPS), MYSQL, RDS, HIVE, ADB_PG, POLAR_PG, POSTGRESQL, and SPARK.

GET_MINUTE

  • Function description

Returns the minute part of a time value.

  • Parameters

    • dateValue: Required. A value of the time data type.

  • Return value

Returns an integer from 0 to 59 that represents the minute. If the dateValue parameter is NULL, this function returns NULL.

  • Supported compute engines

MaxCompute (ODPS), MYSQL, RDS, HIVE, ADB_PG, POLAR_PG, POSTGRESQL, and SPARK.

GET_MONTH

  • Function description

Returns the month part of a time value.

  • Parameters

    • dateValue: Required. A value of the time data type.

  • Return value

Returns an integer from 1 to 12 that represents the month. If the dateValue parameter is NULL, this function returns NULL.

  • Supported compute engines

MaxCompute (ODPS), MYSQL, RDS, HIVE, ADB_PG, POLAR_PG, POSTGRESQL, and SPARK.

GET_QUARTER

  • Function description

Returns the quarter of the year for a time value.

  • Parameters

    • dateValue: Required. A value of the time data type.

  • Return value

Returns an integer from 1 to 4 that represents the quarter. If the dateValue parameter is NULL, this function returns NULL.

  • Supported compute engines

MaxCompute (ODPS), MYSQL, RDS, HIVE, ADB_PG, POLAR_PG, POSTGRESQL, and SPARK.

GET_SECOND

  • Function description

Returns the second part of a time value.

  • Parameters

    • dateValue: Required. A value of the time data type.

  • Return value

Returns an integer from 0 to 59 that represents the second. If the dateValue parameter is NULL, this function returns NULL.

  • Supported compute engines

MaxCompute (ODPS), MYSQL, RDS, HIVE, ADB_PG, POLAR_PG, POSTGRESQL, and SPARK.

GET_WEEKDAY

  • Function description

Returns the day of the week for a specified date.

  • Parameters

    • dateValue: Required. A value of the time data type.

  • Return value

Returns an integer that represents the day of the week. Monday is 0, and the value increments by one for each subsequent day up to Sunday, which is 6. If the dateValue parameter is NULL, this function returns NULL.

  • Supported compute engines

MaxCompute (ODPS), MYSQL, RDS, ADB_PG, POLAR_PG, POSTGRESQL, and SPARK.

GET_WEEK_OF_YEAR

  • Function description

Returns the week of the year for a specified date. Monday is considered the first day of the week. A week that spans two years belongs to the year that contains four or more days of that week. If the week belongs to the previous year, it is considered the last week of that year. If it belongs to the next year, it is considered the first week of that year.

  • Parameters

    • dateValue: Required. A value of the time data type.

  • Return value

Returns an integer that represents the week of the year. If the dateValue parameter is NULL, this function returns NULL.

  • Supported compute engines

MaxCompute (ODPS), MYSQL, RDS, HIVE, ADB_PG, POLAR_PG, POSTGRESQL, and SPARK.

GET_YEAR

  • Function description

Returns the year part of a time value.

  • Parameters

    • dateValue: Required. A value of the time data type.

  • Return value

Returns an integer that represents the year. If the dateValue parameter is NULL, this function returns NULL.

  • Supported compute engines

MaxCompute (ODPS), MYSQL, RDS, HIVE, ADB_PG, POLAR_PG, POSTGRESQL, and SPARK.

NOW

  • Function description

Returns the current system time.

  • Parameters

    • None

  • Return value

Returns the current system time.

  • Supported compute engines

MaxCompute (ODPS), MYSQL, RDS, HIVE, ADB_PG, POLAR_PG, POSTGRESQL, and SPARK.

SUB_DAYS

  • Function description

Subtracts the number of days specified by delta from startdate.

  • Parameters

    • startdate: Required. A value of the time data type.

    • delta: Required. An integer that specifies the number of days to subtract. If delta is greater than 0, days are subtracted. Otherwise, days are added.

  • Return value

Returns the calculated date. The result is truncated to the `yyyy-MM-dd 00:00:00` format. If the startdate or delta parameter is NULL, this function returns NULL.

  • Supported compute engines

MaxCompute (ODPS), MYSQL, RDS, HIVE, ADB_PG, POLAR_PG, POSTGRESQL, and SPARK.

TO_DATE

  • Function description

Converts a string that matches a specified format to a date and time value.

  • Parameters

    • dateString: Required. A string to be converted.

    • format: Required. A string that specifies the date format of dateString. Valid values are yyyy-MM-dd HH:mm:ss, yyyy-MM-dd, yyyyMMdd, and yyyyMMddHHmmss.

  • Return value

Returns the converted date and time value. If the dateString parameter is NULL, this function returns NULL.

  • Supported compute engines

MaxCompute (ODPS), MYSQL, RDS, HIVE, ADB_PG, POLAR_PG, POSTGRESQL, and SPARK.

UNIX_TIMESTAMP

  • Function description

Converts a date and time value to an integer Unix timestamp in seconds.

  • Parameters

    • dateValue: Required. A value of the time data type.

  • Return value

Returns the integer Unix timestamp in seconds. If the dateValue parameter is NULL, this function returns NULL.

  • Supported compute engines

MaxCompute (ODPS), MYSQL, RDS, HIVE, ADB_PG, POLAR_PG, POSTGRESQL, and SPARK.