Python में date और time साथ work करने के लिए built in module datetime use किया जाता है। इस module में आपको वो सभी functions और variables मिल जायँगे जो date और time के लिए important हैं।

Python datetime Example

Copy Fullscreen Close Fullscreen Run
import datetime
cur_date = datetime.datetime.now()
print(cur_date)
Output
C:\Users\Rahulkumar\Desktop\python>python date_time.py
2021-08-21 18:20:34.425163

ऊपर दिए गए example में current date और time show किया गया है। By default date time का format year - month - day hour - minute - seconds - microseconds है।


basically datetime module में datetime name की class ही है जिसका now() method current date , time return करता है।

Getting Only date / time / year / month / day

अब अगर आपको सिर्फ particular date या time या year / month / day चाहिए तो उसके लिए different - different methods हैं जो नीचे example में दिए गए है।

Copy Fullscreen Close Fullscreen Run
import datetime as dt
curr_date = dt.datetime.now()

# current year.
print('Current Year : ', curr_date.year)

# current month.
print('Current Month : ', curr_date.month)

# current daye.
print('Current Day : ', curr_date.day)

# current time.
print('Current Time : ', curr_date.time())
Output
C:\Users\Rahulkumar\Desktop\python>python date_ex.py
Current Year :  2021
Current Month :  8
Current Day :  22
Current Time :  20:48:56.083511

Python strftime()

इसके अलावा datetime class में एक method strftime() भी जो date / time readable form में convert करता है। यह method एक argument accept करता है , जिसमे format specifier pass किया जाता है जिसके according Output return करता है।

Copy Fullscreen Close Fullscreen Run
import datetime as dt
curr_date = dt.datetime.now()
print('Current Year : ', curr_date.strftime('%Y'))
print('Current Month : ', curr_date.strftime('%m'))
print('Current Month Name : ', curr_date.strftime('%B'))
print('Current Day : ', curr_date.strftime('%d'))
print('Current Day Name : ', curr_date.strftime('%A'))
print('Hour Time (0-23) : ', curr_date.strftime('%H'))
print('Hour Time (0-12) : ', curr_date.strftime('%I'))
print('Minute(s) : ', curr_date.strftime('%M'))
print('Second(s) : ', curr_date.strftime('%S'))
print('Microsecond(s) : ', curr_date.strftime('%f'))
print('AM/PM : ', curr_date.strftime('%p'))
print('Local Version Of Date / Time : ', curr_date.strftime('%c'))
Output
C:\Users\Rahulkumar\Desktop\python>python date_ex.py
Current Year :  2021
Current Month :  08
Current Month Name :  August
Current Day :  22
Current Day Name :  Sunday
Hour Time (0-23) :  21
Hour Time (0-12) :  09
Minute(s) :  01
Second(s) :  09
Microsecond(s) :  802546
AM/PM :  PM
Local Version Of Date / Time :  Sun Aug 22 21:01:09 2021

Python datetime formats

Python में format specifier predefined हैं। strftime() में आप अपनी अपनी तरफ से कुछ pass नहीं कर सकते हैं , कुछ main format specifiers इस प्रकार हैं।

FormatDescriptionExample
%aDay name, short versionSun
%ADay name, full versionSunday
%wWeekday as a number 0-6, 0 is Sunday0
%dDay number of month 01-3122
%bMonth name, short versionAug
%BMonth name, full versionAugust
%mMonth number of year, 01-128
%yYear, short version, without century21
%YYear, full version2021
%HHour 00-2321
%IHour 00-1209
%pAM/PMPM
%MMinute 00-5924
%SSecond 00-5908
%fMicrosecond 000000-999999548513
%jDay number of year 001-366234
%UWeek number of year, Sunday as the first day of week, 00-5334
%WWeek number of year, Monday as the first day of week, 00-5333
%cLocal version of date and timeSun Aug 22 21:25:34 2021
%CCentury21
%xLocal version of date08/22/21
%XLocal version of time21:26:13
%GISO 8601 year2021
%uISO 8601 weekday (1-7)7
%VISO 8601 week number (01-53)33

तो ये कुछ predefined format specifiers थे जिनका use आप होनी need के according कर सकते हैं

Python Custom Date

custom date create करने के लिए datetime() class use की जाती है , datetime() class constructor में आठ arguments pass किये जाते हैं।

datetime(year, month, day, hour, minute, seconds, microsecond, tzone)

इनमे से year , month , day ये तीन arguments mandatory होते हैं date create करने के लिए बाकी hour , minute ,seconds , microseconds के लिए by default 0 set होता है, और time zone के लिए बी default none होता है।

Python Custom Date Example

Copy Fullscreen Close Fullscreen Run
import datetime as dt
# custom date. 
custom_date = dt.datetime(2021, 2, 23)
print('Custom Date : ', custom_date)

# date with time.
custom_datetime = dt.datetime(2021, 2, 23, 13, 34,45,2323)
print('Custom DateTime : ',custom_datetime)
Output
C:\Users\Rahulkumar\Desktop\python>python create_date.py
2021-02-23 00:00:00
2021-02-23 13:34:45.002323

I Hope, आपको Python में date & time के बारे में अच्छे से समझ आ गया होगा।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers