1. NOW(): Getting the Current Date and Time
The NOW() function returns the current date and time in the format YYYY-MM-DD HH:MM:SS. It is particularly useful for timestamps and logging events.
Syntax
NOW()
Example
SELECT NOW() AS current_datetime;
Explanation:
NOW()fetches the current system date and time.- The output format is
YYYY-MM-DD HH:MM:SS. - Example output:
2026-04-27 14:35:42.
Practical Use Case
INSERT INTO orders (order_id, customer_id, order_date)
VALUES (101, 1, NOW());
- Automatically stores the current timestamp when a new order is placed.
- Ensures accurate tracking of transactions.
2. CURDATE(): Retrieving the Current Date
While NOW() returns both date and time, CURDATE() returns only the current date in YYYY-MM-DD format. This function is ideal for daily reports or date comparisons.
Syntax
CURDATE()
Example
SELECT CURDATE() AS today_date;
Explanation:
- Returns the current date without the time component.
- Example output:
2026-04-27.
Practical Example
SELECT *
FROM events
WHERE event_date = CURDATE();
- Retrieves all events scheduled for today.
- Useful for dashboards or daily notifications.
3. DATE_FORMAT(): Customizing Date Output
DATE_FORMAT() allows you to display dates in a variety of formats according to your needs. It is highly flexible and widely used for reporting.
Syntax
DATE_FORMAT(date, format)
date: The date value to format.format: The desired output format using specifiers like%Y,%m,%d.
Example
SELECT DATE_FORMAT(NOW(), '%W, %M %d, %Y %h:%i %p') AS formatted_date;
Explanation:
%W→ Full weekday name (e.g., Monday)%M→ Full month name (e.g., April)%d→ Day of the month with leading zeros%Y→ 4-digit year%h:%i %p→ 12-hour time with minutes and AM/PM
Output Example: Sunday, April 27, 2026 02:35 PM
Real-World Use Case
SELECT DATE_FORMAT(order_date, '%d-%m-%Y') AS formatted_order_date
FROM orders;
- Converts the
order_dateintoDD-MM-YYYYformat for invoices or reports.
4. DATEDIFF(): Calculating Date Differences
The DATEDIFF() function calculates the difference in days between two dates. It is essential for age calculations, subscription tracking, and deadline management.
Syntax
DATEDIFF(date1, date2)
- Returns the number of days between
date1anddate2. - If
date1is later thandate2, the result is positive; otherwise, it’s negative.
Example
SELECT DATEDIFF('2026-05-10', '2026-04-27') AS days_difference;
Explanation:
date1=2026-05-10date2=2026-04-27- Difference =
13days
Practical Use Case
SELECT customer_id, DATEDIFF(CURDATE(), last_login) AS days_inactive
FROM users
WHERE DATEDIFF(CURDATE(), last_login) > 30;
- Identifies users who have been inactive for more than 30 days.
- Useful for retention strategies and automated reminders.
Combining Date Functions for Advanced Queries
You can combine these functions to create more sophisticated queries. For example, generating a report of users who registered in the last 7 days with a nicely formatted date:
SELECT user_id,
DATE_FORMAT(registration_date, '%d %b %Y') AS formatted_registration,
DATEDIFF(CURDATE(), registration_date) AS days_since_registration
FROM users
WHERE DATEDIFF(CURDATE(), registration_date) <= 7;
Explanation:
DATE_FORMATmakes the registration date readable.DATEDIFFcalculates how many days ago the registration occurred.WHEREfilters users who joined within the last 7 days.