Lesson 43 of 60 – MySQL Date & Time Functions
72%

MySQL Date & Time Functions

MySQL provides many date and time functions for working with dates, times, years, months, days, and date calculations.

Note: Date and time functions are very useful for student admission dates, payment dates, attendance, reports, birthdays, deadlines, and other time-based data.

1. What are Date & Time Functions?

Date and time functions are built-in MySQL functions used to retrieve, format, extract, and calculate date and time values.

SELECT NOW();

This returns the current date and time.

2. NOW() Function

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

SELECT NOW();

A typical result looks like:

2026-09-21 13:30:25

The actual result depends on the MySQL server's current date and time.

3. CURRENT_TIMESTAMP()

CURRENT_TIMESTAMP() returns the current date and time.

SELECT CURRENT_TIMESTAMP();

It is commonly used when inserting or updating records that need the current timestamp.

INSERT INTO payments
(student_id, amount, payment_date)
VALUES
(101, 5000, CURRENT_TIMESTAMP());

4. CURDATE() Function

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

SELECT CURDATE();

Typical result:

2026-09-21

5. CURRENT_DATE()

CURRENT_DATE() returns the current date.

SELECT CURRENT_DATE();

It is equivalent to using CURDATE().

SELECT CURDATE();
SELECT CURRENT_DATE();

6. CURTIME() Function

The CURTIME() function returns the current time.

SELECT CURTIME();

A typical result may look like:

13:30:25

The exact value changes according to the current time.

7. CURRENT_TIME()

CURRENT_TIME() returns the current time.

SELECT CURRENT_TIME();

It is equivalent to CURTIME().

8. DATE() Function

The DATE() function extracts the date portion from a date-time value.

SELECT DATE('2026-09-21 13:30:25');

Result:

2026-09-21

Example:

SELECT DATE(payment_date)
FROM payments;

9. TIME() Function

The TIME() function extracts the time portion from a date-time value.

SELECT TIME('2026-09-21 13:30:25');

Result:

13:30:25

10. YEAR() Function

The YEAR() function extracts the year from a date.

SELECT YEAR('2026-09-21');

Result:

2026

Example:

SELECT YEAR(admission_date)
FROM students;

11. MONTH() Function

The MONTH() function returns the month number from a date.

SELECT MONTH('2026-09-21');

Result:

9

January is 1 and December is 12.

12. MONTHNAME() Function

The MONTHNAME() function returns the name of the month.

SELECT MONTHNAME('2026-09-21');

Result:

September

13. DAY() Function

The DAY() function returns the day of the month.

SELECT DAY('2026-09-21');

Result:

21

14. DAYNAME() Function

The DAYNAME() function returns the name of the weekday.

SELECT DAYNAME('2026-09-21');

The result is the weekday name corresponding to the supplied date.

15. DAYOFWEEK() Function

The DAYOFWEEK() function returns a number representing the weekday.

SELECT DAYOFWEEK('2026-09-21');

MySQL uses:

  • 1 = Sunday
  • 2 = Monday
  • 3 = Tuesday
  • 4 = Wednesday
  • 5 = Thursday
  • 6 = Friday
  • 7 = Saturday

16. DAYOFYEAR() Function

The DAYOFYEAR() function returns the day number within the year.

SELECT DAYOFYEAR('2026-01-01');

Result:

1

For a date later in the year, the returned number is correspondingly larger.

17. WEEK() Function

The WEEK() function returns the week number for a date.

SELECT WEEK('2026-09-21');

The exact week number depends on the MySQL week mode being used.

18. HOUR() Function

The HOUR() function extracts the hour from a time or date-time value.

SELECT HOUR('13:45:30');

Result:

13

19. MINUTE() Function

The MINUTE() function extracts the minute from a time value.

SELECT MINUTE('13:45:30');

Result:

45

20. SECOND() Function

The SECOND() function extracts the seconds from a time value.

SELECT SECOND('13:45:30');

Result:

30

21. DATE_ADD() Function

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

SELECT DATE_ADD('2026-09-21', INTERVAL 10 DAY);

Result:

2026-10-01

Example:

SELECT DATE_ADD(admission_date, INTERVAL 6 MONTH)
FROM students;

22. DATE_SUB() Function

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

SELECT DATE_SUB('2026-09-21', INTERVAL 10 DAY);

Result:

2026-09-11

23. DATEDIFF() Function

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

SELECT DATEDIFF('2026-09-21', '2026-09-01');

Result:

20

DATEDIFF() considers the date portions and returns the difference in days.

24. TIMESTAMPDIFF() Function

TIMESTAMPDIFF() calculates the difference between two date-time values using a specified unit.

SELECT TIMESTAMPDIFF(
    MONTH,
    '2026-01-01',
    '2026-09-01'
);

Result:

8

It can calculate differences in units such as YEAR, MONTH, DAY, HOUR, MINUTE, and SECOND.

25. DATE_FORMAT() Function

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

SELECT DATE_FORMAT(
    '2026-09-21',
    '%d-%m-%Y'
);

Result:

21-09-2026

Some common format codes are:

  • %d = Day
  • %m = Month number
  • %Y = Four-digit year
  • %M = Month name
  • %W = Weekday name

26. STR_TO_DATE() Function

The STR_TO_DATE() function converts a string into a date or date-time value using a specified format.

SELECT STR_TO_DATE(
    '21-09-2026',
    '%d-%m-%Y'
);

Result:

2026-09-21

This is useful when date values arrive as formatted text.

27. LAST_DAY() Function

The LAST_DAY() function returns the last date of the month for a given date.

SELECT LAST_DAY('2026-09-21');

Result:

2026-09-30

This is useful for monthly reports and billing calculations.

28. Date Functions with WHERE

Date functions can be used with WHERE to filter records by date information.

SELECT *
FROM students
WHERE YEAR(admission_date) = 2026;

This returns students whose admission date is in the year 2026.

Another example:

SELECT *
FROM payments
WHERE MONTH(payment_date) = 9;

29. Common Date & Time Functions

Function Purpose
NOW() Current date and time
CURDATE() Current date
CURTIME() Current time
YEAR() Extract year
MONTH() Extract month
DAY() Extract day
DATE_ADD() Add an interval
DATE_SUB() Subtract an interval
DATEDIFF() Difference between dates
DATE_FORMAT() Format a date

30. Practical Date & Time Functions Example

CREATE TABLE students (
    student_id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    admission_date DATE,
    created_at DATETIME
);

INSERT INTO students
(name, admission_date, created_at)
VALUES
('Amit Kumar', '2026-01-15', '2026-01-15 10:30:00'),
('Priya Singh', '2026-04-20', '2026-04-20 11:15:00'),
('Rahul Sharma', '2026-09-10', '2026-09-10 09:45:00');

-- Display current date
SELECT CURDATE();

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

-- Extract admission year
SELECT
    name,
    YEAR(admission_date) AS admission_year
FROM students;

-- Extract admission month
SELECT
    name,
    MONTH(admission_date) AS admission_month
FROM students;

-- Add six months to admission date
SELECT
    name,
    admission_date,
    DATE_ADD(
        admission_date,
        INTERVAL 6 MONTH
    ) AS course_end_date
FROM students;

-- Find days since admission
SELECT
    name,
    DATEDIFF(CURDATE(), admission_date)
    AS days_since_admission
FROM students;

-- Format admission date
SELECT
    name,
    DATE_FORMAT(
        admission_date,
        '%d-%m-%Y'
    ) AS formatted_date
FROM students;

This example demonstrates how date and time functions can be used in a student management system.

📌 Key Points

  • MySQL provides many functions for working with dates and times.
  • NOW() returns the current date and time.
  • CURDATE() returns the current date.
  • CURTIME() returns the current time.
  • DATE() extracts the date portion from a date-time value.
  • TIME() extracts the time portion.
  • YEAR(), MONTH(), and DAY() extract date components.
  • MONTHNAME() returns the month name.
  • DAYNAME() returns the weekday name.
  • HOUR(), MINUTE(), and SECOND() extract time components.
  • DATE_ADD() adds an interval to a date.
  • DATE_SUB() subtracts an interval from a date.
  • DATEDIFF() calculates the difference between two dates in days.
  • TIMESTAMPDIFF() calculates differences using a specified unit.
  • DATE_FORMAT() formats dates for display.
  • STR_TO_DATE() converts formatted text into a date value.
  • LAST_DAY() returns the last date of a month.

🧠 Quick Quiz

Question: Which MySQL function returns the current date and time?