Current Date and Time
CURDATE(), NOW() and CURRENT_TIMESTAMP give the present moment. Their results change every second, so our examples below use fixed dates. Just to see them work:
Pulling Out Parts
Result:
| order_date | yr | mon | dy | weekday | month_name | qtr |
|---|---|---|---|---|---|---|
| 2025-01-10 | 2025 | 1 | 10 | Friday | January | 1 |
| 2025-04-02 | 2025 | 4 | 2 | Wednesday | April | 2 |
EXTRACT(part FROM date) is the standard SQL way:
Result:
| year_month_value | week_no | dow_sun1 | weekday_mon0 |
|---|---|---|---|
| 202503 | 10 | 7 | 5 |
DAYOFWEEK() returns 1 for Sunday to 7 for Saturday. WEEKDAY() returns 0 for Monday to 6 for Sunday. Check which one your code expects.
Adding and Subtracting Time
Result:
| plus_month | plus_10_days | minus_year | plus_2_weeks |
|---|---|---|---|
| 2025-02-28 | 2025-03-25 | 2024-03-15 | 2025-03-29 |
Notice 2025-01-31 + 1 month becomes 2025-02-28: MySQL clamps to the last valid day of the month.
Differences Between Dates
Result:
| days_between | full_months | age_years | hours |
|---|---|---|---|
| 5 | 2 | 34 | 9 |
DATEDIFF(later, earlier) gives days. TIMESTAMPDIFF(unit, earlier, later) gives whole units, with the earlier date first. Mixing up the argument order is a classic bug: DATEDIFF takes the later date first.
Formatting: DATE_FORMAT
| Code | Meaning | Code | Meaning |
|---|---|---|---|
%Y | 4-digit year | %y | 2-digit year |
%m | Month 01-12 | %c | Month 1-12 |
%d | Day 01-31 | %e | Day 1-31 |
%M | Month name | %b | Short month |
%W | Weekday name | %a | Short weekday |
%H | Hour 00-23 | %i | Minutes |
%s | Seconds | %p | AM or PM |
Result:
| pretty | long_form | clock | year_month_text |
|---|---|---|---|
| 05 Mar 2025 | Wednesday, March 5 | 14:07 | 2025-03 |
Parsing Text into Dates: STR_TO_DATE
STR_TO_DATE(text, format) is the reverse of DATE_FORMAT. Use it for non-ISO input such as 15/03/2025:
Result:
| parsed | parsed_text | impossible_date |
|---|---|---|
| 2025-03-15 | 2025-03-05 | NULL |
An impossible date returns NULL (and a warning) from STR_TO_DATE.
Month Boundaries
Result:
| month_end | month_start | month_start_again | day_of_year |
|---|---|---|---|
| 2025-02-28 | 2025-02-01 | 2025-02-01 | 365 |
Date Ranges the Right Way
Use a half-open range on the bare column so an index can be used and timestamps with a time part work:
Result:
| id | order_date |
|---|---|
| 6 | 2025-04-02 |
| 7 | 2025-04-20 |
Group by month without functions that hide the column in WHERE:
Result:
| month | orders |
|---|---|
| 2025-01 | 1 |
| 2025-02 | 2 |
| 2025-03 | 2 |
| 2025-04 | 2 |
| 2025-05 | 2 |
| 2025-06 | 1 |
DATE, DATETIME and Time Zones
A TIMESTAMP column is stored in UTC and converted to the session time zone when read. A DATETIME is stored exactly as written. Let us prove it with two columns holding the same moment, read in two time zones:
Result:
| session_zone | ts | dt |
|---|---|---|
| UTC session | 2025-03-15 12:00:00 | 2025-03-15 12:00:00 |
Result:
| session_zone | ts | dt |
|---|---|---|
| IST session | 2025-03-15 17:30:00 | 2025-03-15 12:00:00 |
The TIMESTAMP moved from 12:00 to 17:30 when the session zone changed to +05:30, while the DATETIME stayed at 12:00. Neither is wrong, but you must know which one you chose.
DATE for calendar days that have no time zone, such as birthdays.
Common Beginner Mistakes
| Mistake | What happens | Fix |
|---|---|---|
Swapping DATEDIFF arguments | Negative numbers | DATEDIFF(later, earlier) |
BETWEEN on a DATETIME end date | Misses the last day's times | >= start AND < next_day |
Using YEAR(col) = 2025 in WHERE | Index not used | Use a range |
| Mixing time zone expectations | Off-by-hours data | Store UTC |
| Storing dates as text | No functions, bad sorting | Use DATE types |
Assuming + 1 MONTH keeps the day | Jan 31 becomes Feb 28 | Be aware of month ends |
Practice
- Show each order with the weekday name.
- Calculate how many days after joining each customer placed their first order.
- Show the last day of the month of every order date.
- Parse the text
'07-04-2025'as day-month-year withSTR_TO_DATE.
Conclusion
Use DATE_ADD, DATEDIFF, TIMESTAMPDIFF, DATE_FORMAT and STR_TO_DATE for date work, filter with half-open ranges on the bare column, and store moments in UTC. Know the difference between DATETIME and TIMESTAMP.

