Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
MySQL + SQL · Lesson 59

MySQL Date and Time Functions

Choose the Correct Temporal Type

TypeUse
DATECalendar date such as birth date
TIMETime or duration within supported range
DATETIMECalendar date and time without TIMESTAMP session conversion
TIMESTAMPInstant commonly stored with UTC/session conversion

Do not store dates as DD-MM-YYYY text. Native types validate, sort and calculate correctly.

Current Date and Time

SELECT CURRENT_DATE AS today,
       CURRENT_TIME AS current_time,
       NOW() AS current_date_time,
       UTC_TIMESTAMP() AS utc_date_time;

Current functions depend on server/session context. For reproducible examples below, the literal date 2026-08-14 is used.

Extract Date Parts

SELECT DATE('2026-08-14 10:30:00') AS date_only,
       YEAR('2026-08-14') AS year_no,
       MONTH('2026-08-14') AS month_no,
       DAY('2026-08-14') AS day_no,
       MONTHNAME('2026-08-14') AS month_name;
2026-08-14 | 2026 | 8 | 14 | August

Functions on a WHERE column can prevent efficient range index use. Extract for display, but filter indexed timestamps with ranges when possible.

Date Arithmetic

SELECT DATEDIFF('2026-08-21', '2026-08-14') AS days_gap,
       DATE_ADD('2026-08-14', INTERVAL 10 DAY) AS after_10_days,
       DATE_SUB('2026-08-14', INTERVAL 1 MONTH) AS previous_month,
       LAST_DAY('2026-02-10') AS month_end;
7 | 2026-08-24 | 2026-07-14 | 2026-02-28

Month arithmetic can adjust invalid target days; test end-of-month rules for fees, subscriptions and attendance.

Format for Display, Not Storage

SELECT DATE_FORMAT('2026-08-14', '%d-%m-%Y') AS display_date,
       STR_TO_DATE('14-08-2026', '%d-%m-%Y') AS parsed_date;
14-08-2026 | 2026-08-14

Parsing external text should be validated. Store the parsed native value, not the display string.

Safe Ranges and Time Zones

SELECT student_id, full_name, created_at
FROM students
WHERE created_at >= '2026-08-01'
  AND created_at <  '2026-09-01'
ORDER BY created_at;

This includes all of August regardless of time-of-day precision. Define the business time zone, set the session correctly and convert only at system boundaries. Mixing local DATETIME and UTC assumptions causes off-by-one-day reports.

Practice

  1. Find the last day of August 2026.
  2. Add 45 days to 14 August 2026.
  3. Format a stored DATE for Indian display.
  4. Write a half-open range for September 2026.
  5. Explain TIMESTAMP vs DATETIME for an online login event.

Quick Summary

  • Use native temporal types.
  • Current functions use session context.
  • Extract, calculate and format with dedicated functions.
  • Filter time periods with half-open ranges.
  • Define a clear time-zone policy.

Official References

References reviewed 14 August 2026.

Frequently Asked Questions

What is the difference between CURRENT_DATE and NOW()?
CURRENT_DATE returns a date. NOW() returns the current date and time in the session time zone and remains constant within a statement.
How does DATEDIFF work?
DATEDIFF(end, start) returns the number of date boundaries between the date parts, ignoring time components.
Should dates be stored as formatted strings?
No. Store valid DATE, DATETIME or TIMESTAMP values and format only at the presentation boundary.
Why use a half-open date range?
column >= start AND column < next_period avoids guessing the final time or fractional second and includes the full intended period.
Does TIMESTAMP use time-zone conversion?
MySQL converts TIMESTAMP values between the session time zone and UTC for storage/retrieval, while DATETIME generally stores the supplied calendar value without that conversion.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.