Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Handling temporal data is essential for computing delivery estimates, subscription expirations, user ages, and financial reporting periods. MySQL offers an extensive library of Date and Time Functions that allow you to retrieve system clocks, execute calendar arithmetic, and format dates for display.
| Function | Returns | Format |
|---|---|---|
NOW() / CURRENT_TIMESTAMP | Current date and time | 'YYYY-MM-DD HH:MM:SS' |
CURDATE() / CURRENT_DATE | Current date only | 'YYYY-MM-DD' |
CURTIME() / CURRENT_TIME | Current time only | 'HH:MM:SS' |
DATE_ADD() and DATE_SUB()Never perform date arithmetic using simple addition (order_date + 7). Use dedicated date interval functions to account for varying month lengths and leap years:
DATEDIFF()The DATEDIFF(date1, date2) function returns the number of days between two dates (date1 - date2):
TIMESTAMPDIFF(unit, datetime1, datetime2):
SELECT TIMESTAMPDIFF(HOUR, login_time, logout_time) AS hours_online FROM sessions;DATE_FORMAT()When presenting dates on invoices or reports, use DATE_FORMAT(date, format_string):
| Specifier | Description | Example |
|---|---|---|
%Y | 4-digit Year | 2026 |
%y | 2-digit Year | 26 |
%M | Full Month Name | March |
%m | 2-digit Month Number | 03 |
%d | 2-digit Day of Month | 15 |
%W | Full Weekday Name | Sunday |
%h / %i / %p | 12-hour / Minutes / AM-PM | 02:30 PM |
DATEDIFF('2026-03-20', '2026-03-15') return?DATE_FORMAT() outputs the full 4-digit year (e.g., '2026')?birth_date?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Numeric & Math Functions | Control Flow Functions (IF, IFNULL, COALESCE) |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.