
datetime module in python
Date and Time Module Introduction :
नमस्कार दोस्तो आज इस आर्टिकल में जानेंगे कि Python में datetime module क्या हैं ? और यह कितने प्रकार के होते हैं ? हर एक Programming Language में Date और Time का concept होता है | Python में भी Date और Time का concept होता है | Date और time के लिए कुछ modules को import करना पड़ता है | वैसे ही कुछ Modules नीचे दिए गए है |
time Module
datetime Module
calendar Module
time :
time module पे बहुत सारे date और time से related functions, classes और constants होते है |
datetime :
datetime module में भी constants, classes; date और time से related होते है | ये module date और time के साथ काम करने के लिए Object-Oriented Programming में बनाया गया है |
calendar :
Calendar module में calendar से related कुछ functios और classes होते है |
Ticks/Timestamp in Python
12.00am January 1, 1970 से अबतक के बीते हुए seconds को 'Ticks' कहते है |
Python में 12.00am January 1, 1970 से अबतक के बीते हुए seconds को return करने के लिए time module में 'time()' ये function होता है | ये time() function बीते हुए seconds को floating-point number में return करता है |
import time
print("Timestamp/ticks :",time.time())
#Output :
#Timestamp : 1497628217.2675047
What is epoch in Python?
import time
print("epoch :",time.gmtime(0))
Output :epoch :
time.struct_time(
tm_year=1970,
tm_mon=1,
tm_mday=1,
tm_hour=0,
tm_min=0,
tm_sec=0,
tm_wday=3,
tm_yday=1,
tm_isdst=0)
Time Module in Python
time module में date और time से related कुछ functions और constants होते है | time module में कुछ classes C या C++ में बने है | निचे दिए हुए example में gmtime() function से C या C++ में बने हुए 'struct_time' इस object को return किया गया है |
import time
print(time.localtime())
Output :time.struct_time(
tm_year=2023,
tm_mon=1,
tm_mday=12,
tm_hour=17,
tm_min=4,
tm_sec=32,
tm_wday=4,
tm_yday=167,
tm_isdst=0)
struct_time structure में कुछ attributes होते है |Index | Attribute | Values |
---|---|---|
0 | tm_year | Year in 4 Digit(for eg, 2017) |
1 | tm_mon | 1 to 12(Months) |
2 | tm_mday | 1 to 28,29,30,31(Days) |
3 | tm_hour | 0 to 23(Hours) |
4 | tm_min | 0 to 59(Minutes) |
5 | tm_sec | 0 to 61 (60 or 61 are leap-seconds) |
6 | tm_wday | 0 to 6(Weekday) |
7 | tm_yday | 1 to 366(Yearday) |
8 | tm_isdst | DST(Daylight Saving Time) 0, if not DST 1, if DST, -1, if not known about DST |
How to Get Current Date and Time using time Module ?
Example पर localtime() द्वारा current time को display किया गया है लेकिन इस return हुए time को आसानी से पढ़ा नहीं जा सकता है |
import time
print("Current Time :",time.localtime())
Output :Current Time : time.struct_time(tm_year=2023, tm_mon=1, tm_mday=12, tm_hour=23, tm_min=16, tm_sec=7, tm_wday=5, tm_yday=168, tm_isdst=0)
Getting Current Date and Time in readable Format using time Module
Readable Format में current time को return करना हो तो 'asctime()' function का इस्तेमाल किया जाता है |
import time
print(time.asctime())
Output :Thu Jan 12 23:38:53 2023
Important Attributes of 'time' Module
'time' Module Attribute | Description |
---|---|
altzone | ये local DST timezone का offset; seconds में return करता है | |
daylight | अगर DST timezone defined होता है तो यहाँ पर Nonzero value होती है | |
timezone | local(non-DST) timezone का offset seconds में return करता है | |
tzname | local(non-DST) timezone और local DST timezone के साथ या उसके बिना tuple में return होते है | |
Important Functions of 'time' Module
'time' Module Function | Description |
---|---|
asctime() | दिए हुए time tuple को या gmtime() या localtime() से return होनेवाले struct_time को string में convert करके return करता है | |
ctime() | epoch local time(Jan 1 05:30:00 1970) और दिए गए seconds उसमे add करके उस time को string में return किया जाता है | |
gmtime() | epoch GMT time(Jan 1 00:00:00 1970) और दिए गए seconds उसमे add करके उस time को struct_time में return किया जाता है | |
localtime() | epoch local time(Jan 1 05:30:00 1970) और दिए गए seconds उसमे add करके उस time को struct_time में return किया जाता है | |
mktime() | दिए हुए time-tuple या struct_time को floating-point value को seconds में return करता है | |
sleep() | इस function का इस्तेमाल calling thread को suspend करने के लिए किया जाता है | |
strftime() | time-tuple या 'gmtime() या localtime()' से return होनेवाले 'struct_time' और दिए हुए format से 'time' को return किया जाता है | |
strptime() | दिए हुए format के अनुसार string को parse करके 'struct_time' में return किया जाता है | |
time() | epoch GMT time(Jan 1, 1970, 00:00:00) से लेकर बीते हुए time को seconds(floating-point number) मे return करता है | |