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

MySQL Date और Time Functions

सही Temporal Type चुनें

TypeUse
DATEBirth date जैसी calendar date
TIMESupported range में time/duration
DATETIMETIMESTAMP conversion बिना calendar date-time
TIMESTAMPUTC/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;
2026-08-14 | 2026 | 8 | 14 | August

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;
7 | 2026-08-24 | 2026-07-14 | 2026-02-28

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;
14-08-2026 | 2026-08-14

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 गलत हो सकती हैं।

अभ्यास

  1. August 2026 का last day खोजें।
  2. 14 August में 45 days जोड़ें।
  3. Stored DATE को Indian display में format करें।
  4. September 2026 half-open range लिखें।
  5. 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 किए गए।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

CURRENT_DATE और NOW() में क्या अंतर है?
CURRENT_DATE date देता है। NOW() session time zone में current date-time देता और one statement के भीतर constant रहता है।
DATEDIFF कैसे काम करता है?
DATEDIFF(end, start) time parts ignore करके date parts के बीच day boundaries का number देता है।
क्या dates formatted strings में store करें?
नहीं। Valid DATE, DATETIME या TIMESTAMP store करें और presentation boundary पर format करें।
Half-open date range क्यों लें?
column >= start AND column < next_period final time guess किए बिना complete intended period शामिल करता है।
क्या TIMESTAMP time-zone conversion करता है?
MySQL TIMESTAMP को session time zone और UTC के बीच convert करता है; DATETIME सामान्यतः supplied calendar value बिना उस conversion के रखता है।
🔗

Share this topic with a friend

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

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

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

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.