Python : Date and Time
Python , Date और Time मे तीन modules होते है |
- time modules
- datetime modules
- calendar Module
- What is Ticks/Timestamp in Python ?
January 1, 1970 , 12.00am से अबतक के बीते हुए seconds को ‘Ticks’ कहते है | इन seconds को return करने के लिए time module में ‘time()’ function होता है | ये time() function बीते हुए seconds को floating-point number में return करता है |
For Example :-
import time
print("Ticks :--",time.time())
OutPut :-
Ticks :-- 1581776049.4683158
- What is epoch in Python ?
January 1, 1970 , 12.00am को epoch कहा जाता है | Python में time module के ‘gmtime()’ function से epoch को return किया जाता है |
For Example :-
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)
- How to get the current date and time?
time module मे localtime() फंक्शन का use करके current time / date निकलते है |
# time module
# using localtime () function
import time;
print(time.localtime(time.time()))
OutPut :-
time.struct_time(tm_year=2020, tm_mon=2, tm_mday=16, tm_hour=12, tm_min=20, tm_sec=10, tm_wday=6, tm_yday=47, tm_isdst=0)
- How to get the current date and time in in readable Format ?
#time module
# using asctime () function
import time
print(time.asctime())
OutPut :-
Sun Feb 16 12:23:37 2020
- python sleep time
इस function का इस्तेमाल calling thread को suspend करने के लिए किया जाता है |
#time module
# using sleep () function
import time
for i in range(11,15):
print(i)
#Each element will be printed after 1 second
time.sleep(2)
OutPut :-
11
12
13
14
- Creating date objects
# datetime module
import datetime;
# returns the datetime object for the specified time
print(datetime.datetime(2020, 2, 16, 14, 15, 10))
OutPut :-
2020-02-16 14:15:10
- Printing the calendar of whole year
# calendar module
import calendar
# printing the calendar of the year 2019
calendar.prcal(2020)
- Printing the calendar of February month
# calendar module
import calendar;
cal = calendar.month(2020,2)
#printing the calendar of February 2020
print(cal)
OutPut :-
February 2020
Mo Tu We Th Fr Sa Su
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29