TExcelTAD
Home › MySQL › MySQL Date Functions

MySQL Date Functions

This reference lesson collects the date and time functions you'll use most. All examples use fixed dates; in real code you'd often pass CURDATE() or NOW() instead.

Current date and time

Function Returns
CURDATE(), CURRENT_DATE today's date
CURTIME(), CURRENT_TIME the current time
NOW(), CURRENT_TIMESTAMP the current date and time (fixed for the whole statement)
SYSDATE() the date and time at the exact moment it is evaluated
UTC_DATE(), UTC_TIMESTAMP() the same in UTC

These depend on the clock, so they aren't run here.

Extracting parts

Function '2025-03-09 14:05:30' gives
YEAR(d), MONTH(d), DAY(d) 2025, 3, 9
HOUR(t), MINUTE(t), SECOND(t) 14, 5, 30
DAYNAME(d), MONTHNAME(d) Sunday, March
DAYOFWEEK(d) 1 (1 = Sunday … 7 = Saturday)
WEEKDAY(d) 6 (0 = Monday … 6 = Sunday)
DAYOFYEAR(d), QUARTER(d) 68, 1
DATE(dt), TIME(dt) 2025-03-09, 14:05:30
EXTRACT(unit FROM d) e.g. EXTRACT(YEAR_MONTH FROM d) = 202503
mysql · try it yourself
SELECT order_id, order_date,
       YEAR(order_date) AS y, MONTH(order_date) AS m, DAY(order_date) AS d,
       DAYNAME(order_date) AS weekday,
       QUARTER(order_date) AS q
FROM orders
WHERE order_id IN (1, 9, 22);
+----------+------------+------+------+------+---------+------+
| order_id | order_date | y    | m    | d    | weekday | q    |
+----------+------------+------+------+------+---------+------+
|        1 | 2025-01-05 | 2025 |    1 |    5 | Sunday  |    1 |
|        9 | 2025-03-03 | 2025 |    3 |    3 | Monday  |    1 |
|       22 | 2025-06-03 | 2025 |    6 |    3 | Tuesday |    2 |
+----------+------------+------+------+------+---------+------+

A typical use is grouping by period:

mysql · try it yourself
SELECT QUARTER(order_date) AS quarter, COUNT(*) AS orders
FROM orders
WHERE YEAR(order_date) = 2025
GROUP BY quarter
ORDER BY quarter;
+---------+--------+
| quarter | orders |
+---------+--------+
|       1 |     12 |
|       2 |     10 |
+---------+--------+

Formatting: DATE_FORMAT

DATE_FORMAT(d, format) turns a date into text. The most common specifiers:

Specifier Meaning Example
%Y / %y year, 4 / 2 digits 2025 / 25
%m / %c month 01–12 / 1–12 03 / 3
%M / %b month name / abbreviation March / Mar
%d / %e day 01–31 / 1–31 09 / 9
%W / %a weekday name / abbreviation Sunday / Sun
%H / %h hour 00–23 / 01–12 14 / 02
%i / %s minutes / seconds 05 / 30
%p AM or PM PM
mysql · try it yourself
SELECT DATE_FORMAT('2025-03-09 14:05:30', '%d/%m/%Y') AS european,
       DATE_FORMAT('2025-03-09 14:05:30', '%M %e, %Y') AS us_long,
       DATE_FORMAT('2025-03-09 14:05:30', '%a %d %b %y, %h:%i %p') AS short_12h,
       DATE_FORMAT('2025-03-09', '%Y-%m') AS month_key;
+------------+---------------+-------------------------+-----------+
| european   | us_long       | short_12h               | month_key |
+------------+---------------+-------------------------+-----------+
| 09/03/2025 | March 9, 2025 | Sun 09 Mar 25, 02:05 PM | 2025-03   |
+------------+---------------+-------------------------+-----------+

The reverse, text to date, is STR_TO_DATE(text, format) with the same specifiers.

Note: Month and day names come out in English unless you change the session's lc_time_names (for example SET lc_time_names = 'de_DE';).

Adding and subtracting

Function Example Result
DATE_ADD(d, INTERVAL n unit) / d + INTERVAL n unit '2025-03-09' + INTERVAL 10 DAY 2025-03-19
DATE_SUB(d, INTERVAL n unit) / d - INTERVAL n unit '2025-03-09' - INTERVAL 1 WEEK 2025-03-02
ADDTIME(dt, t) / SUBTIME ADDTIME('10:00:00', '01:30:00') 11:30:00
LAST_DAY(d) LAST_DAY('2024-02-10') 2024-02-29
MAKEDATE(year, day_of_year) MAKEDATE(2025, 100) 2025-04-10

Units: MICROSECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR, and combined ones such as DAY_HOUR (INTERVAL '1 6' DAY_HOUR).

mysql · try it yourself
SELECT order_date,
       order_date + INTERVAL 14 DAY AS payment_due,
       order_date - INTERVAL 1 MONTH AS month_before,
       LAST_DAY(order_date) AS month_end,
       DATE_FORMAT(order_date, '%Y-%m-01') AS month_start,
       ADDTIME('2025-03-09 22:30:00', '03:15:00') AS late_shift_end
FROM orders
WHERE order_id = 7;
+------------+-------------+--------------+------------+-------------+---------------------+
| order_date | payment_due | month_before | month_end  | month_start | late_shift_end      |
+------------+-------------+--------------+------------+-------------+---------------------+
| 2025-02-14 | 2025-02-28  | 2025-01-14   | 2025-02-28 | 2025-02-01  | 2025-03-10 01:45:00 |
+------------+-------------+--------------+------------+-------------+---------------------+

Differences between dates

Function Returns
DATEDIFF(a, b) days from b to a (ignores time of day)
TIMESTAMPDIFF(unit, start, end) complete units from start to end
TIMEDIFF(a, b) a TIME value, a - b
mysql · try it yourself
SELECT first_name, hire_date,
       DATEDIFF('2025-06-30', hire_date) AS days_employed,
       TIMESTAMPDIFF(YEAR, hire_date, '2025-06-30') AS full_years,
       TIMESTAMPDIFF(MONTH, hire_date, '2025-06-30') AS full_months
FROM employees
WHERE employee_id IN (1, 5, 8)
ORDER BY hire_date;
+------------+------------+---------------+------------+-------------+
| first_name | hire_date  | days_employed | full_years | full_months |
+------------+------------+---------------+------------+-------------+
| Grace      | 2019-03-01 |          2313 |          6 |          75 |
| Maya       | 2022-09-12 |          1022 |          2 |          33 |
| Sara       | 2024-05-06 |           420 |          1 |          13 |
+------------+------------+---------------+------------+-------------+
mysql · try it yourself
SELECT TIMEDIFF('18:30:00', '09:15:00') AS shift_length,
       TIMESTAMPDIFF(MINUTE, '2025-03-09 09:15:00', '2025-03-09 18:30:00') AS minutes,
       SEC_TO_TIME(3725) AS from_seconds,
       TIME_TO_SEC('01:02:05') AS to_seconds;
+--------------+---------+--------------+------------+
| shift_length | minutes | from_seconds | to_seconds |
+--------------+---------+--------------+------------+
| 09:15:00     |     555 | 01:02:05     |       3725 |
+--------------+---------+--------------+------------+

Unix timestamps and time zones

UNIX_TIMESTAMP(dt) gives the seconds since 1970-01-01 00:00:00 UTC; FROM_UNIXTIME(n) goes back. CONVERT_TZ(dt, from, to) shifts a value between time zones. Both depend on the session time zone, which is UTC in these examples:

mysql · try it yourself
SELECT UNIX_TIMESTAMP('2025-01-01 00:00:00') AS epoch_seconds,
       FROM_UNIXTIME(1735689600) AS back_again,
       CONVERT_TZ('2025-03-09 12:00:00', '+00:00', '+05:30') AS in_india;
+---------------+---------------------+---------------------+
| epoch_seconds | back_again          | in_india            |
+---------------+---------------------+---------------------+
|    1735689600 | 2025-01-01 00:00:00 | 2025-03-09 17:30:00 |
+---------------+---------------------+---------------------+

Named zones such as 'Europe/Berlin' work only after the server's time zone tables have been loaded (mysql_tzinfo_to_sql); numeric offsets always work.

Test yourself

What does DATEDIFF('2025-03-01', '2025-02-01') return?

  1. 1
  2. 28
  3. -28
  4. 30

Which format string produces '09/03/2025' from 2025-03-09?

  1. '%m/%d/%Y'
  2. '%d/%m/%Y'
  3. '%D/%M/%y'
  4. 'dd/mm/yyyy'

What does LAST_DAY('2025-02-10') return?

  1. 2025-02-28
  2. 2025-02-29
  3. 2025-03-01
  4. 28