00:00

MySQL Date and Time

Last updated:

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

TypeFormatRangeUse for
DATEYYYY-MM-DD1000-01-01 … 9999-12-31Check-in, birthdays
TIMEHH:MM:SS-838:59:59 … 838:59:59Opening hours, durations
DATETIMEYYYY-MM-DD HH:MM:SS1000 … 9999Events at a local wall-clock time
TIMESTAMPYYYY-MM-DD HH:MM:SS1970-01-01 … 2038-01-19 UTCMoments in time; stored as UTC, shown in the session time zone
YEARYYYY1901 … 2155Model or batch years

Table: bookings (5 rows)

idguestcheck_incheck_outbooked_atpaid_at
1Aanya2026-03-142026-03-162026-02-01 10:15:002026-02-01 10:20:00
2Boris2026-03-202026-03-272026-02-15 22:40:002026-02-16 08:05:00
3Chloe2026-04-022026-04-032026-03-10 07:05:00NULL
4Dhruv2026-04-102026-04-152026-03-12 18:30:002026-03-12 18:31:00
5Elif2026-05-012026-05-042026-04-01 12:00:002026-04-02 09:00:00
Always write literals as '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

query.sql
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_datenow_lengthsane_clock
1191
FunctionReturns
NOW() / CURRENT_TIMESTAMPCurrent 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)
The live values change every second, so this page checks properties of them rather than printing them.

3. Extracting Parts

Parts of a DATETIME value and the functions that extract themThe value 2026-03-14 09:45:30 split into year 2026, month 03, day 14, hour 09, minute 45 and second 30, each labelled with the function that returns it: YEAR, MONTH, DAY, HOUR, MINUTE, SECOND. DATE() returns the date part and TIME() the time part.2026YEAR()-03MONTH()-14DAY()09HOUR():45MINUTE():30SECOND()DATE() → 2026-03-14TIME() → 09:45:30DAYNAME() → SaturdayWEEK(), QUARTER() …
Figure 1: each part of a date/time value has a function that extracts it.
extract.sql
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

guestcheck_inyrmondyweekdayqtrbooked_hour
Aanya2026-03-142026314Saturday110
Boris2026-03-202026320Friday122
Chloe2026-04-02202642Thursday27
Dhruv2026-04-102026410Friday218
Elif2026-05-01202651Friday212
query.sql
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

ymdt
2026032026-03-1409:45:30

4. Formatting with DATE_FORMAT

format.sql
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

guestshort_datelong_datebooked
Aanya14 Mar 2026Saturday, 14th March01/02/2026 10:15 AM
Boris20 Mar 2026Friday, 20th March15/02/2026 10:40 PM
Chloe02 Apr 2026Thursday, 2nd April10/03/2026 07:05 AM
Dhruv10 Apr 2026Friday, 10th April12/03/2026 06:30 PM
Elif01 May 2026Friday, 1st May01/04/2026 12:00 PM
SpecifierMeaningExample
%Y / %y4- / 2-digit year2026 / 26
%m / %b / %MMonth number / short / full name03 / Mar / March
%d / %DDay / day with suffix14 / 14th
%W / %aWeekday full / shortSaturday / Sat
%H / %hHour 00–23 / 01–1222 / 10
%i / %s / %pMinutes / seconds / AM-PM40 / 00 / PM

5. Parsing Text with STR_TO_DATE

Convert text in any layout into a real date, using the same specifiers:

query.sql
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_formatfrom_text
2026-03-142026-03-20 22:30:00
query.sql
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

arithmetic.sql
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

guestcheck_innext_daytwo_weeks_laterreminder_datemonth_end
Aanya2026-03-142026-03-152026-03-282026-03-112026-03-31
Boris2026-03-202026-03-212026-04-032026-03-172026-03-31
Chloe2026-04-022026-04-032026-04-162026-03-302026-04-30
query.sql
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_monthleap_plus_year
2026-02-282029-02-28
When the target month is shorter, MySQL uses its last day: 31 January + 1 month is 28 February.

7. Differences Between Dates

difference.sql
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

guestnightsdays_booked_aheadminutes_to_paymonths_since_jan15
Aanya24151
Boris7335652
Chloe123NULL2
Dhruv52912
Elif33012603

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

query.sql
SELECT guest, check_in FROM bookings
WHERE check_in >= '2026-04-01' AND check_in < '2026-05-01';   -- all of April

Result: 2 rows

guestcheck_in
Chloe2026-04-02
Dhruv2026-04-10
query.sql
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

guestbooked_at
Dhruv2026-03-12 18:30:00
Use half-open ranges (>= 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

by-month.sql
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

monthbookingsnights_sold
2026-0329
2026-0426
2026-0513
query.sql
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_dayarrivals
Thursday1
Friday3
Saturday1

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:

time-zones.sql
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_atpaid_at
2026-02-01 10:15:002026-02-01 10:20:00

Session in IST (+05:30)

booked_atpaid_at
2026-02-01 10:15:002026-02-01 15:50:00
query.sql
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.
solution.sql
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

guestcheckout_dayhas_saturday_night
AanyaMonday1
BorisFriday1
ChloeFriday0
DhruvWednesday1
ElifMonday1
Exercise 2: Bookings made more than 30 days before check-in.
solution.sql
SELECT guest, DATE(booked_at) AS booked, check_in
FROM bookings
WHERE DATEDIFF(check_in, booked_at) > 30;

Result: 2 rows

guestbookedcheck_in
Aanya2026-02-012026-03-14
Boris2026-02-152026-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