MySQL Date और Time Functions
सही Temporal Type चुनें
| Type | Use |
|---|---|
| DATE | Birth date जैसी calendar date |
| TIME | Supported range में time/duration |
| DATETIME | TIMESTAMP conversion बिना calendar date-time |
| TIMESTAMP | UTC/session conversion वाला instant |
Dates को DD-MM-YYYY text में store न करें। Native types validate, sort और calculate सही करते हैं।
Current Date और Time
SELECT CURRENT_DATE AS today,
CURRENT_TIME AS current_time,
NOW() AS current_date_time,
UTC_TIMESTAMP() AS utc_date_time;Current functions server/session context पर depend हैं। Reproducible examples में 2026-08-14 literal लिया है।
Date Parts Extract करें
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;WHERE column पर function efficient range index use रोक सकता है। Display के लिए extract, filtering में ranges लें।
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;Month arithmetic invalid target day adjust कर सकता है; fee/subscription में month-end rules test करें।
Display Format, 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;External text validate करें। Parsed native value store करें, display string नहीं।
Safe Ranges और 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;यह time precision से independent पूरा August शामिल करता है। Business time zone define करें और session सही set करें। Local DATETIME तथा UTC assumptions mix करने से reports one-day गलत हो सकती हैं।
अभ्यास
- August 2026 का last day खोजें।
- 14 August में 45 days जोड़ें।
- Stored DATE को Indian display में format करें।
- September 2026 half-open range लिखें।
- Online login event में TIMESTAMP vs DATETIME समझाएँ।
त्वरित सारांश
- Native temporal types लें।
- Current functions session context use करते हैं।
- Dedicated functions से extract/calculate/format करें।
- Half-open ranges से periods filter करें।
- Clear time-zone policy रखें।
Official संदर्भ
References 14 August 2026 को review किए गए।
