← Back to MySQL Course | Chapter 13: Date & Numeric Functions | Lesson 5 of 6

DATEDIFF और DATE_ADD

DATEDIFF count करता है कि दो dates के बीच कितने दिन हैं, आपके birthday की एक countdown जैसा, और DATE_ADD किसी date को आपकी चुनी गई amount आगे move करता है।
Syntax
sql
DATEDIFF(end_date, start_date)
DATE_ADD(date, INTERVAL value unit)
DATE_SUB(date, INTERVAL value unit)

DATEDIFF से Days Between ढूँढना

DATEDIFF दो dates के बीच whole संख्या में days calculate करता है, अगर पहली date दूसरी से पहले आए तो एक negative number return करते हुए — एक invoice कितने दिन overdue है जैसी चीज़ें measure करने का एक तेज़ तरीका।

उदाहरण: Finding Days Between with DATEDIFF

sql
SELECT DATEDIFF('2024-06-15', '2024-06-01') AS days_overdue;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

DATE_ADD से Days Add करना

DATE_ADD एक date value में एक चुना गया interval — days, weeks, months, या years — add करता है, जो एक subscription renewal date या एक shipping estimate जैसी चीज़ें compute करने का standard तरीका है।

उदाहरण: Adding Days with DATE_ADD

sql
SELECT DATE_ADD('2024-06-01', INTERVAL 30 DAY) AS renewal_date;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

DATE_SUB से Days Subtract करना

DATE_SUB DATE_ADD जैसा ही काम करता है लेकिन आगे के बजाय समय में पीछे move करता है, आज के relative 'पिछले 30 दिनों में placed orders' जैसी चीज़ें calculate करने के लिए उपयोगी।

उदाहरण: Subtracting Days with DATE_SUB

sql
SELECT DATE_SUB('2024-06-30', INTERVAL 30 DAY) AS thirty_days_before;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Time Calculations

DATE_ADD और DATE_SUB दोनों पूरी datetime values पर operate कर सकते हैं, आपको whole days से ज़्यादा precision चाहिए होने पर hours, minutes, या seconds add या subtract करने देते हुए।

उदाहरण: Time Calculations

sql
SELECT DATE_ADD('2024-06-01 10:00:00', INTERVAL 90 MINUTE) AS meeting_end;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Age या Time Left Calculate करना

DATEDIFF को NOW() के साथ combine करना आपको एक live countdown देता है, जैसे एक deadline तक कितने दिन बचे हैं या किसी customer के last order के बाद से कितने दिन बीत गए, हर query पर fresh calculate किए गए।

उदाहरण: Calculating Age or Time Left

sql
CREATE TABLE customers (id INT, last_order_date DATE);
INSERT INTO customers VALUES (1, '2024-05-01');
SELECT id, DATEDIFF(NOW(), last_order_date) AS days_since_last_order FROM customers;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. DATEDIFF के arguments reverse करना, क्योंकि DATEDIFF(a, b) a - b है और एक negative number देता है।
  2. DATEDIFF इस्तेमाल करना और time part के मायने रखने की उम्मीद करना, जबकि यह सिर्फ date हिस्सा इस्तेमाल करता है।
  3. INTERVAL 30 DAYS को plural में लिखना, जबकि unit DAY है।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.