← Back to Chapters

MySQL Date Functions

? MySQL Date Functions

? Quick Overview

MySQL Date Functions are used to work with date and time values. They help you retrieve the current date, extract parts of a date, perform date calculations, and format date values for reports and queries.

? Key Concepts

  • Dates are stored in DATE, DATETIME, TIMESTAMP formats
  • Date functions can extract year, month, day, hour, etc.
  • They are commonly used in filtering, reporting, and calculations

? Syntax / Theory

  • CURDATE() – Returns current date
  • NOW() – Returns current date and time
  • DATE() – Extracts date part from datetime
  • YEAR(), MONTH(), DAY() – Extract date parts
  • DATE_ADD(), DATE_SUB() – Add or subtract dates
  • DATEDIFF() – Difference between two dates

? Code Examples

? View Code Example
-- Get current system date
SELECT CURDATE();

-- Get current date and time
SELECT NOW();

-- Extract year from a date
SELECT YEAR('2025-08-15');

-- Add 10 days to a given date
SELECT DATE_ADD('2025-08-15', INTERVAL 10 DAY);

-- Find difference between two dates
SELECT DATEDIFF('2025-08-15', '2025-08-01');

? Interactive Playground

Select a function and click Run...

? Live Output / Explanation

Result Explanation

  • CURDATE() → Returns today’s date (YYYY-MM-DD)
  • NOW() → Returns current date and time
  • YEAR() → Extracts the year part
  • DATE_ADD() → Calculates a future date
  • DATEDIFF() → Returns number of days between dates

✅ Tips & Best Practices

  • Use CURDATE() instead of NOW() when time is not required
  • Always store dates in proper DATE or DATETIME format
  • Use date functions instead of manual calculations

? Try It Yourself

  • Find users registered in the last 7 days
  • Calculate age using date of birth
  • Filter records by current month