MySQL Date and Time
Almost every application stores dates: when an order was placed, a booking's check-in, a user's birthday. MySQL has dedicated date and time types and a rich set of functions to read, format, compare and calculate with them. This tutorial uses a small hotel bookings table to show the ones you will use most.
1. Date and Time Types
| Type | Format | Range | Use for |
|---|---|---|---|
DATE | YYYY-MM-DD | 1000-01-01 … 9999-12-31 | Check-in, birthdays |
TIME | HH:MM:SS | -838:59:59 … 838:59:59 | Opening hours, durations |
DATETIME | YYYY-MM-DD HH:MM:SS | 1000 … 9999 | Events at a local wall-clock time |
TIMESTAMP | YYYY-MM-DD HH:MM:SS | 1970-01-01 … 2038-01-19 UTC | Moments in time; stored as UTC, shown in the session time zone |
YEAR | YYYY | 1901 … 2155 | Model or batch years |
Table: bookings (5 rows)
| id | guest | check_in | check_out | booked_at | paid_at |
|---|---|---|---|---|---|
| 1 | Aanya | 2026-03-14 | 2026-03-16 | 2026-02-01 10:15:00 | 2026-02-01 10:20:00 |
| 2 | Boris | 2026-03-20 | 2026-03-27 | 2026-02-15 22:40:00 | 2026-02-16 08:05:00 |
| 3 | Chloe | 2026-04-02 | 2026-04-03 | 2026-03-10 07:05:00 | NULL |
| 4 | Dhruv | 2026-04-10 | 2026-04-15 | 2026-03-12 18:30:00 | 2026-03-12 18:31:00 |
| 5 | Elif | 2026-05-01 | 2026-05-04 | 2026-04-01 12:00:00 | 2026-04-02 09:00:00 |
'YYYY-MM-DD' and 'YYYY-MM-DD HH:MM:SS'. Other formats like '14/03/2026' are not dates to MySQL.2. Current Date and Time
SELECT CURDATE() IS NOT NULL AS has_date, CHAR_LENGTH(NOW()) AS now_length, UTC_TIMESTAMP() >= '2026-01-01' AS sane_clock;
Result: 1 row
| has_date | now_length | sane_clock |
|---|---|---|
| 1 | 19 | 1 |
| Function | Returns |
|---|---|
NOW() / CURRENT_TIMESTAMP | Current date and time (start of the statement) |
CURDATE() | Today's date |
CURTIME() | Current time |
UTC_TIMESTAMP() | Current date and time in UTC |
SYSDATE() | Time the function actually runs (can differ inside one statement) |
3. Extracting Parts
SELECT guest, check_in,
YEAR(check_in) AS yr, MONTH(check_in) AS mon, DAY(check_in) AS dy,
DAYNAME(check_in) AS weekday, QUARTER(check_in) AS qtr,
HOUR(booked_at) AS booked_hour
FROM bookings;
Result: 5 rows
| guest | check_in | yr | mon | dy | weekday | qtr | booked_hour |
|---|---|---|---|---|---|---|---|
| Aanya | 2026-03-14 | 2026 | 3 | 14 | Saturday | 1 | 10 |
| Boris | 2026-03-20 | 2026 | 3 | 20 | Friday | 1 | 22 |
| Chloe | 2026-04-02 | 2026 | 4 | 2 | Thursday | 2 | 7 |
| Dhruv | 2026-04-10 | 2026 | 4 | 10 | Friday | 2 | 18 |
| Elif | 2026-05-01 | 2026 | 5 | 1 | Friday | 2 | 12 |
SELECT EXTRACT(YEAR_MONTH FROM '2026-03-14') AS ym, DATE('2026-03-14 09:45:30') AS d, TIME('2026-03-14 09:45:30') AS t;
Result: 1 row
| ym | d | t |
|---|---|---|
| 202603 | 2026-03-14 | 09:45:30 |
4. Formatting with DATE_FORMAT
SELECT guest,
DATE_FORMAT(check_in, '%d %b %Y') AS short_date,
DATE_FORMAT(check_in, '%W, %D %M') AS long_date,
DATE_FORMAT(booked_at, '%d/%m/%Y %h:%i %p') AS booked
FROM bookings;
Result: 5 rows
| guest | short_date | long_date | booked |
|---|---|---|---|
| Aanya | 14 Mar 2026 | Saturday, 14th March | 01/02/2026 10:15 AM |
| Boris | 20 Mar 2026 | Friday, 20th March | 15/02/2026 10:40 PM |
| Chloe | 02 Apr 2026 | Thursday, 2nd April | 10/03/2026 07:05 AM |
| Dhruv | 10 Apr 2026 | Friday, 10th April | 12/03/2026 06:30 PM |
| Elif | 01 May 2026 | Friday, 1st May | 01/04/2026 12:00 PM |
| Specifier | Meaning | Example |
|---|---|---|
%Y / %y | 4- / 2-digit year | 2026 / 26 |
%m / %b / %M | Month number / short / full name | 03 / Mar / March |
%d / %D | Day / day with suffix | 14 / 14th |
%W / %a | Weekday full / short | Saturday / Sat |
%H / %h | Hour 00–23 / 01–12 | 22 / 10 |
%i / %s / %p | Minutes / seconds / AM-PM | 40 / 00 / PM |
5. Parsing Text with STR_TO_DATE
Convert text in any layout into a real date, using the same specifiers:
SELECT STR_TO_DATE('14/03/2026', '%d/%m/%Y') AS from_indian_format,
STR_TO_DATE('March 20, 2026 10:30 PM', '%M %d, %Y %h:%i %p') AS from_text;
Result: 1 row
| from_indian_format | from_text |
|---|---|
| 2026-03-14 | 2026-03-20 22:30:00 |
SELECT STR_TO_DATE('31/02/2026', '%d/%m/%Y') AS invalid_date; -- 31 Feb doesn't exist → NULL
Result: 1 row
| invalid_date |
|---|
| NULL |
6. Adding and Subtracting Time
SELECT guest, check_in,
check_in + INTERVAL 1 DAY AS next_day,
DATE_ADD(check_in, INTERVAL 2 WEEK) AS two_weeks_later,
DATE_SUB(check_in, INTERVAL 3 DAY) AS reminder_date,
LAST_DAY(check_in) AS month_end
FROM bookings WHERE id <= 3;
Result: 3 rows
| guest | check_in | next_day | two_weeks_later | reminder_date | month_end |
|---|---|---|---|---|---|
| Aanya | 2026-03-14 | 2026-03-15 | 2026-03-28 | 2026-03-11 | 2026-03-31 |
| Boris | 2026-03-20 | 2026-03-21 | 2026-04-03 | 2026-03-17 | 2026-03-31 |
| Chloe | 2026-04-02 | 2026-04-03 | 2026-04-16 | 2026-03-30 | 2026-04-30 |
SELECT '2026-01-31' + INTERVAL 1 MONTH AS jan31_plus_month, '2028-02-29' + INTERVAL 1 YEAR AS leap_plus_year;
Result: 1 row
| jan31_plus_month | leap_plus_year |
|---|---|
| 2026-02-28 | 2029-02-28 |
7. Differences Between Dates
SELECT guest,
DATEDIFF(check_out, check_in) AS nights,
DATEDIFF(check_in, DATE(booked_at)) AS days_booked_ahead,
TIMESTAMPDIFF(MINUTE, booked_at, paid_at) AS minutes_to_pay,
TIMESTAMPDIFF(MONTH, '2026-01-15', check_in) AS months_since_jan15
FROM bookings;
Result: 5 rows
| guest | nights | days_booked_ahead | minutes_to_pay | months_since_jan15 |
|---|---|---|---|---|
| Aanya | 2 | 41 | 5 | 1 |
| Boris | 7 | 33 | 565 | 2 |
| Chloe | 1 | 23 | NULL | 2 |
| Dhruv | 5 | 29 | 1 | 2 |
| Elif | 3 | 30 | 1260 | 3 |
DATEDIFF counts days; TIMESTAMPDIFF(unit, start, end) works in any unit (SECOND … YEAR) and returns whole units. Chloe's NULL payment gives NULL.
8. Filtering by Date Ranges
SELECT guest, check_in FROM bookings
WHERE check_in >= '2026-04-01' AND check_in < '2026-05-01'; -- all of April
Result: 2 rows
| guest | check_in |
|---|---|
| Chloe | 2026-04-02 |
| Dhruv | 2026-04-10 |
SELECT guest, booked_at FROM bookings
WHERE booked_at >= '2026-03-12' AND booked_at < '2026-03-13'; -- one whole day of DATETIMEs
Result: 1 row
| guest | booked_at |
|---|---|
| Dhruv | 2026-03-12 18:30:00 |
>= start, < next start). They include the whole last day and let MySQL use an index. WHERE MONTH(check_in) = 4 works too but can't use an index.9. Grouping by Month or Weekday
SELECT DATE_FORMAT(check_in, '%Y-%m') AS month,
COUNT(*) AS bookings,
SUM(DATEDIFF(check_out, check_in)) AS nights_sold
FROM bookings
GROUP BY month
ORDER BY month;
Result: 3 rows
| month | bookings | nights_sold |
|---|---|---|
| 2026-03 | 2 | 9 |
| 2026-04 | 2 | 6 |
| 2026-05 | 1 | 3 |
SELECT DAYNAME(check_in) AS arrival_day, COUNT(*) AS arrivals FROM bookings GROUP BY arrival_day, DAYOFWEEK(check_in) ORDER BY DAYOFWEEK(check_in);
Result: 3 rows
| arrival_day | arrivals |
|---|---|
| Thursday | 1 |
| Friday | 3 |
| Saturday | 1 |
10. Time Zones: DATETIME vs TIMESTAMP
A TIMESTAMP is stored in UTC and shown in the session time zone; a DATETIME is shown exactly as stored. Watch Aanya's payment time change with the session zone while her booking time stays put:
SET time_zone = '+00:00';
SELECT booked_at, paid_at FROM bookings WHERE id = 1;
SET time_zone = '+05:30';
SELECT booked_at, paid_at FROM bookings WHERE id = 1;
Session in UTC
| booked_at | paid_at |
|---|---|
| 2026-02-01 10:15:00 | 2026-02-01 10:20:00 |
Session in IST (+05:30)
| booked_at | paid_at |
|---|---|
| 2026-02-01 10:15:00 | 2026-02-01 15:50:00 |
SELECT CONVERT_TZ('2026-03-14 09:00:00', '+00:00', '+05:30') AS utc_to_ist;
Result: 1 row
| utc_to_ist |
|---|
| 2026-03-14 14:30:00 |
11. Practice Exercises
Exercise 1: Show each booking's check-out day name and whether the stay includes a Saturday night.
SELECT guest, DAYNAME(check_out) AS checkout_day,
EXISTS (SELECT 1 FROM (SELECT 0 AS n UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6) d
WHERE d.n < DATEDIFF(check_out, check_in) AND DAYOFWEEK(check_in + INTERVAL d.n DAY) = 7) AS has_saturday_night
FROM bookings;
Result: 5 rows
| guest | checkout_day | has_saturday_night |
|---|---|---|
| Aanya | Monday | 1 |
| Boris | Friday | 1 |
| Chloe | Friday | 0 |
| Dhruv | Wednesday | 1 |
| Elif | Monday | 1 |
Exercise 2: Bookings made more than 30 days before check-in.
SELECT guest, DATE(booked_at) AS booked, check_in
FROM bookings
WHERE DATEDIFF(check_in, booked_at) > 30;
Result: 2 rows
| guest | booked | check_in |
|---|---|---|
| Aanya | 2026-02-01 | 2026-03-14 |
| Boris | 2026-02-15 | 2026-03-20 |
12. Summary
- Use DATE, TIME, DATETIME, TIMESTAMP and YEAR; write literals as 'YYYY-MM-DD [HH:MM:SS]'.
- NOW(), CURDATE(), UTC_TIMESTAMP() give the current time; YEAR(), MONTH(), DAYNAME()… extract parts.
- DATE_FORMAT formats, STR_TO_DATE parses; INTERVAL arithmetic adds and subtracts.
- DATEDIFF and TIMESTAMPDIFF measure differences; filter with half-open ranges.
- TIMESTAMP follows the session time zone, DATETIME doesn't.
Reference: Date and Time Functions – MySQL Reference Manual
https://dev.mysql.com/doc/refman/9.7/en/date-and-time-functions.html