Databases 4 min read

Master MySQL Date Functions: CURDATE, NOW, DATE_FORMAT, DATEDIFF, and More

This guide explains MySQL’s most useful date and time functions—including CURDATE(), NOW(), DATE_FORMAT(), DATEDIFF(), DATE_ADD(), DATE_SUB(), and DATE()—with clear syntax examples and sample results, helping developers manipulate temporal data efficiently in their databases.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master MySQL Date Functions: CURDATE, NOW, DATE_FORMAT, DATEDIFF, and More

MySQL provides a rich set of date and time functions for handling temporal data. This article introduces the most commonly used functions with syntax and example output.

CURDATE() – Get Current Date

The CURDATE() function returns the current date without time.

SELECT CURDATE();
+------------+
| CURDATE()  |
+------------+
| 2024-01-18 |
+------------+

NOW() – Get Current Date and Time

The NOW() function returns the current date and time.

SELECT NOW();
+---------------------+
| NOW()               |
+---------------------+
| 2024-01-18 13:30:45 |
+---------------------+

DATE_FORMAT() – Format Date

The DATE_FORMAT() function formats a date value according to a format string.

SELECT DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s') AS formatted_date;
+---------------------+
| formatted_date      |
+---------------------+
| 2024-01-18 13:30:45 |
+---------------------+

DATEDIFF() – Calculate Date Difference

The DATEDIFF() function returns the number of days between two dates.

SELECT DATEDIFF('2024-01-20', '2024-01-18') AS date_difference;
+------------------+
| date_difference |
+------------------+
| 2                |
+------------------+

DATE_ADD() – Add Interval to Date

The DATE_ADD() function adds a time interval to a date.

SELECT DATE_ADD(NOW(), INTERVAL 7 DAY) AS future_date;
+---------------------+
| future_date         |
+---------------------+
| 2024-01-25 13:30:45 |
+---------------------+

DATE_SUB() – Subtract Interval from Date

The DATE_SUB() function subtracts a time interval from a date.

SELECT DATE_SUB(NOW(), INTERVAL 3 MONTH) AS past_date;
+---------------------+
| past_date           |
+---------------------+
| 2023-10-18 13:30:45 |
+---------------------+

DATE() – Extract Date Part

The DATE() function extracts the date part from a datetime value.

SELECT DATE(NOW()) AS extracted_date;
+---------------------+
| extracted_date      |
+---------------------+
| 2024-01-18          |
+---------------------+

By using these functions appropriately, developers can simplify date handling in MySQL, improve query readability, and increase data processing efficiency.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

SQLdatabasemysqlTime ManipulationDate Functions
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.