← Back to MySQL Course | Chapter 3: Data Types | Lesson 3 of 8

Date और Time Data Types

Date और time types आपकी table के लिए calendars और clocks जैसे हैं: कुछ सिर्फ day याद रखते हैं, कुछ day plus exact time।
Syntax
sql
column_name DATE       -- 'YYYY-MM-DD'
column_name TIME       -- 'HH:MM:SS'
column_name DATETIME   -- 'YYYY-MM-DD HH:MM:SS'
column_name TIMESTAMP

DATE Type

DATE बिना किसी time component के YYYY-MM-DD format में सिर्फ एक calendar day store करता है, जो एक birthday या hire date जैसी values के लिए सही fit है जहाँ clock time irrelevant है।

उदाहरण: DATE Type

sql
CREATE TABLE people (birthday DATE);
INSERT INTO people VALUES ('1990-05-14');

TIME Type

TIME HH:MM:SS format में एक clock value store करता है और एक specific moment के बजाय एक duration या interval भी represent कर सकता है, जैसे '02:30:00' का मतलब ढाई घंटे।

उदाहरण: TIME Type

sql
CREATE TABLE tasks (duration TIME);
INSERT INTO tasks VALUES ('02:30:00');

DATETIME और TIMESTAMP

DATETIME बिना किसी timezone conversion के exactly enter की गई तरह एक combined calendar date और time store करता है, जबकि TIMESTAMP save पर internally UTC में convert करता है और पढ़ने पर वापस session की timezone में — multi-timezone apps के लिए एक subtle लेकिन important अंतर।

उदाहरण: DATETIME and TIMESTAMP

sql
CREATE TABLE events (
  starts_at DATETIME,
  logged_at TIMESTAMP
);

YEAR Type

YEAR एक compact 1-byte column है जो सिर्फ एक year value store करता है (जैसे 2024), जो DATE से ज़्यादा space-efficient है जब आपको genuinely सिर्फ एक year track करनी हो, जैसे किसी car का model year।

उदाहरण: YEAR Type

sql
CREATE TABLE cars (model_year YEAR);
INSERT INTO cars VALUES (2024);

Date Formatting

Built-in functions आपको यह reformat करने देते हैं कि एक stored date कैसे display हो (2024-03-05 के बजाय 'March 5, 2024') या 30 days add करने जैसा date arithmetic perform करने देते हैं, underlying stored value बदले बिना।

उदाहरण: Date Formatting

sql
SELECT DATE_FORMAT('2024-03-05', '%M %d, %Y') AS formatted;
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. एक date को 14-05-1990 की तरह insert करना, जबकि MySQL को YYYY-MM-DD format चाहिए।
  2. 2038 साल के बाद की dates के लिए TIMESTAMP चुनना, जब इसकी range वहीं खत्म हो जाती है और DATETIME आगे तक जाता है।
  3. TIME इस्तेमाल करना और सिर्फ दिन के एक time की उम्मीद करना, जबकि यह 24 घंटों से आगे की durations भी रख सकता है।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 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.