Date and time functions
The date and time functions of Raft DB: current time, parts of a date, arithmetic with intervals, formatting and parsing.
Dates and times are kept as text in the form MySQL writes them (YYYY-MM-DD, HH:MM:SS, YYYY-MM-DD HH:MM:SS[.ffffff]), and the date functions read that text. A value that is not a date, and NULL, give NULL.
What to know before using them:
- Time zone. Raft DB runs in UTC.
NOW(),CURDATE(),UNIX_TIMESTAMP()and the others use UTC whatevertime_zonesays, andCONVERT_TZdoes not convert. The comparison servers were in another zone, so these functions are not compared with them. - Whole seconds. The functions below compute with whole seconds. A
DATETIME(6)column keeps its microseconds (see Date and time types), butMICROSECOND()and%fdo not read them. - Interval units.
INTERVAL expr unitacceptsMICROSECOND,SECOND,MINUTE,HOUR,DAY,WEEK,MONTH,QUARTERandYEAR.date + INTERVAL 1 DAYanddate - INTERVAL 1 DAYwork as operators. - Zero dates.
0000-00-00is accepted when thesql_modedoes not haveNO_ZERO_DATE(see SQL modes);YEAR('0000-00-00 00:00:00')isNULL.
Summary of functions#
| Function | What it does | Dialect |
|---|---|---|
ADDDATE | Adds an interval, or a number of days, to date. | MySQL and MariaDB |
ADDTIME | Adds the time expr2 to the time or datetime expr1. | MySQL and MariaDB |
ADD_MONTHS | Adds n months to date. | MariaDB function; Raft DB also accepts it when it runs as MySQL |
CONVERT_TZ | Accepted so that applications run, but it does not convert: it returns dt unchanged. | MySQL and MariaDB |
CURDATE | Returns the current date as YYYY-MM-DD, in UTC. | MySQL and MariaDB |
CURTIME | Returns the current time as HH:MM:SS, in UTC. | MySQL and MariaDB |
DATE | Returns the date part of a date or datetime. | MySQL and MariaDB |
DATEDIFF | Returns expr1 - expr2 in days; only the date parts are used. | MySQL and MariaDB |
DATE_ADD | Adds an interval to a date or datetime. | MySQL and MariaDB |
DATE_FORMAT | Writes date as the format says. | MySQL and MariaDB |
DATE_SUB | Subtracts an interval from a date or datetime. | MySQL and MariaDB |
DAY | Returns the day of the month, 1 to 31. | MySQL and MariaDB |
DAYNAME | Returns the name of the weekday, in English. | MySQL and MariaDB |
DAYOFWEEK | Returns the weekday index: 1 is Sunday, 7 is Saturday. | MySQL and MariaDB |
DAYOFYEAR | Returns the day of the year, 1 to 366. | MySQL and MariaDB |
EXTRACT | Returns one part of date. | MySQL and MariaDB |
FROM_DAYS | Returns the date that is n days from year 0. | MySQL and MariaDB |
FROM_UNIXTIME | Turns a count of seconds after 1970-01-01 00:00:00 UTC into a datetime, given as text, or formatted with format the way DATE_FORMAT does it. | MySQL and MariaDB |
HOUR | Returns the hour of a time or datetime. | MySQL and MariaDB |
LAST_DAY | Returns the last day of the month of date. | MySQL and MariaDB |
LOCALTIME | Synonyms of NOW. | MySQL and MariaDB |
MAKEDATE | Returns the date for a year and a day of the year. | MySQL and MariaDB |
MAKETIME | Returns the time made of the three parts. | MySQL and MariaDB |
MICROSECOND | Accepted; always returns 0, because the date functions work with whole seconds. | MySQL and MariaDB |
MINUTE | Returns the minute of a time or datetime, 0 to 59. | MySQL and MariaDB |
MONTH | Returns the month of date, 1 to 12. | MySQL and MariaDB |
MONTHNAME | Returns the name of the month, in English. | MySQL and MariaDB |
NOW | Returns the current date and time as YYYY-MM-DD HH:MM:SS, in UTC, with fsp (0 to 6) fractional digits. | MySQL and MariaDB |
PERIOD_ADD | Adds n months to a period written YYMM or YYYYMM; returns YYYYMM. | MySQL and MariaDB |
PERIOD_DIFF | Returns the number of months between two periods written YYMM or YYYYMM. | MySQL and MariaDB |
QUARTER | Returns the quarter of the year, 1 to 4. | MySQL and MariaDB |
SECOND | Returns the second of a time or datetime, 0 to 59. | MySQL and MariaDB |
SEC_TO_TIME | Returns a number of seconds as HH:MM:SS. | MySQL and MariaDB |
STR_TO_DATE | Reads str as a date or datetime written the way format says (the specifiers %Y %y %m %c %d %e %H %k %h %I %l %i %s %S %p %M %b %%). | MySQL and MariaDB |
SUBDATE | Subtracts an interval, or a number of days, from date. | MySQL and MariaDB |
SUBTIME | Subtracts the time expr2 from the time or datetime expr1. | MySQL and MariaDB |
TIME | Returns the time part of a datetime. | MySQL and MariaDB |
TIMEDIFF | Returns expr1 - expr2 as a time. | MySQL and MariaDB |
TIMESTAMP | Returns expr as a datetime (a date gets the time 00:00:00). | MySQL and MariaDB |
TIMESTAMPADD | Adds interval units to datetime. | MySQL and MariaDB |
TIMESTAMPDIFF | Returns datetime2 - datetime1 in whole units (SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR). | MySQL and MariaDB |
TIME_FORMAT | Writes a time as format says (%H %k %h %I %l %i %s %S %p %r %T %f %%). | MySQL and MariaDB |
TIME_TO_SEC | Returns a time as a number of seconds. | MySQL and MariaDB |
TO_DAYS | Returns the number of days from year 0 to date. | MySQL and MariaDB |
TO_SECONDS | Returns the number of seconds from year 0 to a date or datetime. | MySQL and MariaDB |
UNIX_TIMESTAMP | Without an argument returns the seconds since 1970-01-01 00:00:00 UTC now; with one, the seconds of that date (taken as UTC). | MySQL and MariaDB |
UTC_DATE | Returns the current UTC date. | MySQL and MariaDB |
UTC_TIME | Returns the current UTC time. | MySQL and MariaDB |
UTC_TIMESTAMP | Returns the current UTC date and time. | MySQL and MariaDB |
WEEK | Returns the week of the year, 0 to 53, counted as mode 0 does (weeks start on Sunday; week 1 is the first with a Sunday in the year). | MySQL and MariaDB |
WEEKDAY | Returns the weekday index: 0 is Monday, 6 is Sunday. | MySQL and MariaDB |
WEEKOFYEAR | Returns the ISO calendar week, 1 to 53. | MySQL and MariaDB |
YEAR | Returns the year of date. | MySQL and MariaDB |
YEARWEEK | Returns the year and week (YYYYWW) as WEEK mode 0 counts them. | MySQL and MariaDB |
ADDDATE#
ADDDATE(date, INTERVAL expr unit)
ADDDATE(date, days)Adds an interval, or a number of days, to date. Synonym of DATE_ADD for the first form.
Example:
SELECT ADDDATE('2024-01-31', 1);Result: 2024-02-01
ADDTIME#
ADDTIME(expr1, expr2)Adds the time expr2 to the time or datetime expr1.
Example:
SELECT ADDTIME('01:00:00', '02:30:00');Result: 03:30:00
ADD_MONTHS#
MariaDB function; Raft DB also accepts it when it runs as MySQL.
ADD_MONTHS(date, n)Adds n months to date. The last day of a month stays the last day of the month it moves to.
Example:
SELECT ADD_MONTHS('2012-01-31', 1);Result: 2012-02-29
CONVERT_TZ#
CONVERT_TZ(dt, from_tz, to_tz)Accepted so that applications run, but it does not convert: it returns dt unchanged. Raft DB runs in UTC and has no time zone tables.
Example:
SELECT CONVERT_TZ('2024-01-01 12:00:00', '+00:00', '+05:30');Result: 2024-01-01 12:00:00 (MySQL gives 2024-01-01 17:30:00)
CURDATE#
CURDATE()
CURRENT_DATE
CURRENT_DATE()Returns the current date as YYYY-MM-DD, in UTC.
Example:
SELECT CURDATE();Result: a date such as 2026-11-12
CURTIME#
CURTIME()
CURRENT_TIME
CURRENT_TIME()Returns the current time as HH:MM:SS, in UTC.
Example:
SELECT CURTIME();Result: a time such as 14:05:09
DATE#
DATE(expr)Returns the date part of a date or datetime.
Example:
SELECT DATE('2003-12-31 01:02:03');Result: 2003-12-31
DATEDIFF#
DATEDIFF(expr1, expr2)Returns expr1 - expr2 in days; only the date parts are used.
Example:
SELECT DATEDIFF('2024-03-01', '2024-02-01');Result: 29
DATE_ADD#
DATE_ADD(date, INTERVAL expr unit)
date + INTERVAL expr unitAdds an interval to a date or datetime. The units are MICROSECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER and YEAR; the compound units (DAY_HOUR, YEAR_MONTH, ...) are not supported. If the day does not exist in the new month, the last day of that month is used. The result has a time part if date has one or the unit is a time unit.
Example:
SELECT DATE_ADD('2024-01-31 10:00:00', INTERVAL 1 MONTH);Result: 2024-02-29 10:00:00
DATE_FORMAT#
DATE_FORMAT(date, format)Writes date as the format says. Supported specifiers: %Y %y %m %c %d %e %H %k %h %I %l %i %s %S %p %r %T %M %b %W %a %w %j %D %%. %f always writes 000000: fractions of a second are not kept by the date functions. Other specifiers (%U, %u, %V, %v, %X, %x) are written as the letter.
Example:
SELECT DATE_FORMAT('2024-05-06 17:08:09', '%W %M %e, %Y %l:%i %p %j');Result: Monday May 6, 2024 5:08 PM 127
DATE_SUB#
DATE_SUB(date, INTERVAL expr unit)
date - INTERVAL expr unitSubtracts an interval from a date or datetime. Units as for DATE_ADD.
Example:
SELECT DATE_SUB('2024-03-01', INTERVAL 1 DAY);Result: 2024-02-29
DAY#
DAY(date)
DAYOFMONTH(date)Returns the day of the month, 1 to 31. DAYOFMONTH is a synonym.
Example:
SELECT DAY('2024-05-06');Result: 6
DAYNAME#
DAYNAME(date)Returns the name of the weekday, in English.
Example:
SELECT DAYNAME('2024-05-06');Result: Monday
DAYOFWEEK#
DAYOFWEEK(date)Returns the weekday index: 1 is Sunday, 7 is Saturday.
Example:
SELECT DAYOFWEEK('2024-05-06');Result: 2
DAYOFYEAR#
DAYOFYEAR(date)Returns the day of the year, 1 to 366.
Example:
SELECT DAYOFYEAR('2024-05-06');Result: 127
EXTRACT#
EXTRACT(unit FROM date)Returns one part of date. The units that are one word (YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, QUARTER, WEEK, MICROSECOND) are supported; the compound units (YEAR_MONTH, DAY_HOUR, ...) are not.
Example:
SELECT EXTRACT(YEAR FROM '2019-07-02');Result: 2019
FROM_DAYS#
FROM_DAYS(n)Returns the date that is n days from year 0. Days up to 365 give NULL in MySQL and 0000-00-00 in MariaDB.
Example:
SELECT FROM_DAYS(730669);Result: 2000-07-03
FROM_UNIXTIME#
FROM_UNIXTIME(unix_timestamp [, format])Turns a count of seconds after 1970-01-01 00:00:00 UTC into a datetime, given as text, or formatted with format the way DATE_FORMAT does it.
Example:
SELECT FROM_UNIXTIME(86400);Result: 1970-01-02 00:00:00
HOUR#
HOUR(time)Returns the hour of a time or datetime.
Example:
SELECT HOUR('2024-05-06 10:05:03');Result: 10
LAST_DAY#
LAST_DAY(date)Returns the last day of the month of date.
Example:
SELECT LAST_DAY('2024-02-10');Result: 2024-02-29
LOCALTIME#
LOCALTIME [(fsp)]
LOCALTIMESTAMP [(fsp)]Synonyms of NOW.
Example:
SELECT LOCALTIME;Result: the current datetime
MAKEDATE#
MAKEDATE(year, dayofyear)Returns the date for a year and a day of the year. NULL if the day is 0 or less.
Example:
SELECT MAKEDATE(2011, 31);Result: 2011-01-31
MAKETIME#
MAKETIME(hour, minute, second)Returns the time made of the three parts.
Example:
SELECT MAKETIME(12, 15, 30);Result: 12:15:30
MICROSECOND#
MICROSECOND(expr)Accepted; always returns 0, because the date functions work with whole seconds.
Example:
SELECT MICROSECOND('2024-05-06 10:05:03.123456');Result: 0 (MySQL gives 123456)
MINUTE#
MINUTE(time)Returns the minute of a time or datetime, 0 to 59.
Example:
SELECT MINUTE('2024-05-06 10:05:03');Result: 5
MONTH#
MONTH(date)Returns the month of date, 1 to 12.
Example:
SELECT MONTH('2024-05-06');Result: 5
MONTHNAME#
MONTHNAME(date)Returns the name of the month, in English.
Example:
SELECT MONTHNAME('2024-05-06');Result: May
NOW#
NOW([fsp])
CURRENT_TIMESTAMP [(fsp)]
SYSDATE([fsp])Returns the current date and time as YYYY-MM-DD HH:MM:SS, in UTC, with fsp (0 to 6) fractional digits. CURRENT_TIMESTAMP and SYSDATE are synonyms; SYSDATE is not evaluated at the time it runs, but, like NOW, once for each row.
Example:
SELECT NOW(3) LIKE '____-__-__ __:__:__.___';Result: 1
PERIOD_ADD#
PERIOD_ADD(period, n)Adds n months to a period written YYMM or YYYYMM; returns YYYYMM.
Example:
SELECT PERIOD_ADD(200801, 2);Result: 200803
PERIOD_DIFF#
PERIOD_DIFF(p1, p2)Returns the number of months between two periods written YYMM or YYYYMM.
Example:
SELECT PERIOD_DIFF(200802, 200703);Result: 11
QUARTER#
QUARTER(date)Returns the quarter of the year, 1 to 4.
Example:
SELECT QUARTER('2024-05-06');Result: 2
SECOND#
SECOND(time)Returns the second of a time or datetime, 0 to 59.
Example:
SELECT SECOND('2024-05-06 10:05:03');Result: 3
SEC_TO_TIME#
SEC_TO_TIME(seconds)Returns a number of seconds as HH:MM:SS.
Example:
SELECT SEC_TO_TIME(3661);Result: 01:01:01
STR_TO_DATE#
STR_TO_DATE(str, format)Reads str as a date or datetime written the way format says (the specifiers %Y %y %m %c %d %e %H %k %h %I %l %i %s %S %p %M %b %%). The result is a datetime when the format has an hour, minute or second, a date otherwise. NULL if str does not fit the format.
Example:
SELECT STR_TO_DATE('06/05/2024 17:08', '%d/%m/%Y %H:%i');Result: 2024-05-06 17:08:00
SUBDATE#
SUBDATE(date, INTERVAL expr unit)
SUBDATE(date, days)Subtracts an interval, or a number of days, from date. Synonym of DATE_SUB for the first form.
Example:
SELECT SUBDATE('2024-03-01', 1);Result: 2024-02-29
SUBTIME#
SUBTIME(expr1, expr2)Subtracts the time expr2 from the time or datetime expr1.
Example:
SELECT SUBTIME('01:00:00', '02:30:00');Result: -01:30:00
TIME#
TIME(expr)Returns the time part of a datetime.
Example:
SELECT TIME('2003-12-31 01:02:03');Result: 01:02:03
TIMEDIFF#
TIMEDIFF(expr1, expr2)Returns expr1 - expr2 as a time.
Example:
SELECT TIMEDIFF('12:00:00', '10:30:00');Result: 01:30:00
TIMESTAMP#
TIMESTAMP(expr)Returns expr as a datetime (a date gets the time 00:00:00). The two-argument form TIMESTAMP(expr, time) is accepted and the second argument is ignored.
Example:
SELECT TIMESTAMP('2003-12-31');Result: 2003-12-31 00:00:00
TIMESTAMPADD#
TIMESTAMPADD(unit, interval, datetime)Adds interval units to datetime. Units as for DATE_ADD.
Example:
SELECT TIMESTAMPADD(MINUTE, 15, '2024-05-06 10:00:00');Result: 2024-05-06 10:15:00
TIMESTAMPDIFF#
TIMESTAMPDIFF(unit, datetime1, datetime2)Returns datetime2 - datetime1 in whole units (SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR).
Example:
SELECT TIMESTAMPDIFF(MONTH, '2024-01-31', '2024-03-30');Result: 1
TIME_FORMAT#
TIME_FORMAT(time, format)Writes a time as format says (%H %k %h %I %l %i %s %S %p %r %T %f %%). Hours may be more than 23.
Example:
SELECT TIME_FORMAT('100:00:00', '%H %k %h %I %l');Result: 100 100 04 04 4
TIME_TO_SEC#
TIME_TO_SEC(time)Returns a time as a number of seconds.
Example:
SELECT TIME_TO_SEC('01:01:01');Result: 3661
TO_DAYS#
TO_DAYS(date)Returns the number of days from year 0 to date.
Example:
SELECT TO_DAYS('2008-10-07');Result: 733687
TO_SECONDS#
TO_SECONDS(expr)Returns the number of seconds from year 0 to a date or datetime.
Example:
SELECT TO_SECONDS('2009-11-29');Result: 63426672000
UNIX_TIMESTAMP#
UNIX_TIMESTAMP([date])Without an argument returns the seconds since 1970-01-01 00:00:00 UTC now; with one, the seconds of that date (taken as UTC). Returns 0 for a value that is not a date.
Example:
SELECT UNIX_TIMESTAMP('1970-01-02 00:00:00');Result: 86400
UTC_DATE#
UTC_DATE()Returns the current UTC date. The same as CURDATE(), as Raft DB runs in UTC.
Example:
SELECT UTC_DATE();Result: a date such as 2026-11-12
UTC_TIME#
UTC_TIME()Returns the current UTC time. The same as CURTIME().
Example:
SELECT UTC_TIME();Result: a time such as 14:05:09
UTC_TIMESTAMP#
UTC_TIMESTAMP()Returns the current UTC date and time. The same as NOW().
Example:
SELECT UTC_TIMESTAMP();Result: a datetime
WEEK#
WEEK(date [, mode])Returns the week of the year, 0 to 53, counted as mode 0 does (weeks start on Sunday; week 1 is the first with a Sunday in the year). The mode argument is accepted and ignored.
Example:
SELECT WEEK('2008-02-20');Result: 7
WEEKDAY#
WEEKDAY(date)Returns the weekday index: 0 is Monday, 6 is Sunday.
Example:
SELECT WEEKDAY('2024-05-06');Result: 0
WEEKOFYEAR#
WEEKOFYEAR(date)Returns the ISO calendar week, 1 to 53.
Example:
SELECT WEEKOFYEAR('2008-02-20');Result: 8
YEAR#
YEAR(date)Returns the year of date. NULL for the zero date 0000-00-00.
Example:
SELECT YEAR('2024-05-06 07:08:09');Result: 2024
YEARWEEK#
YEARWEEK(date [, mode])Returns the year and week (YYYYWW) as WEEK mode 0 counts them. The mode argument is accepted and ignored.
Example:
SELECT YEARWEEK('1987-01-01');Result: 198652