MySQL LAST_DAY() Function
MySQL LAST_DAY(): Find the Final Calendar Day of Any Month
The LAST_DAY() function accepts a DATE or DATETIME value and returns a DATE representing the final day of that month ('YYYY-MM-DD'). It handles 28-, 29-, 30-, and 31-day months automatically, including leap-year Februaries.
Subscription billing and accounting queries use LAST_DAY() to close monthly ledgers and calculate month-end prorated charges.
Syntax
Supply one date or datetime expression:
LAST_DAY(date_value)
date_value: ADATE,DATETIME,TIMESTAMP, or valid date string.
If the input is a DATETIME, LAST_DAY() drops the time portion and returns a pure DATE.
Practical Example
Suppose your finance team computes the month-end statement date and remaining days in the month for rows in demo_orders.
SELECT
order_id,
order_date,
LAST_DAY(order_date) AS statement_close_date,
DATEDIFF(LAST_DAY(order_date), order_date) AS days_until_month_end
FROM demo_orders
WHERE order_id IN (101, 102, 103);
| order_id | order_date | statement_close_date | days_until_month_end |
|---|---|---|---|
| 101 | 2024-02-10 | 2024-02-29 | 19 |
| 102 | 2026-02-10 | 2026-02-28 | 18 |
| 103 | 2026-04-15 | 2026-04-30 | 15 |
Notice how row 101 (leap year 2024) returns 2024-02-29, while row 102 (2026) returns 2026-02-28.
Common Mistakes & NULL Behavior
Avoid these errors when using LAST_DAY():
NULLand invalid dates returnNULL: PassingNULL,'2026-02-30', or'2026-00-15'makesLAST_DAY()returnNULL.- No built-in
FIRST_DAY()function: MySQL does not have aFIRST_DAY()function. To get the first day of the same month, writeDATE_FORMAT(order_date, '%Y-%m-01')orDATE_ADD(LAST_DAY(DATE_SUB(order_date, INTERVAL 1 MONTH)), INTERVAL 1 DAY). - Time portion is stripped: Comparing
created_at <= LAST_DAY(created_at)on aDATETIMEcolumn misses rows after00:00:00on the last day! Usecreated_at < LAST_DAY(created_at) + INTERVAL 1 DAYinstead. - No
TIME-only input: Passing'15:00:00'without a date returnsNULL.
Frequently Asked Questions
How do I find the last day of the previous month?
Run LAST_DAY(DATE_SUB(CURDATE(), INTERVAL 1 MONTH)) or LAST_DAY(CURDATE() - INTERVAL 1 MONTH).
How do I get the first day of the current month using LAST_DAY()?
Subtract one month, take LAST_DAY(), and add one day: LAST_DAY(CURDATE() - INTERVAL 1 MONTH) + INTERVAL 1 DAY.
Does LAST_DAY() return a DATE or a DATETIME?
It always returns a DATE (YYYY-MM-DD), even if you pass a DATETIME or TIMESTAMP input.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.