Dates and Times
Dates
Calendar date values are represented with the date class. Instances have attributes for year, month, and day. It is easy to create a date representing the current date using the today()
The following is example code:
import datetime
today = datetime.date.today()
print('Today is {}'.format(today))
print('ctime :', today.ctime())
tt = today.timetuple()
print('tuple : tm_year =', tt.tm_year)
print(' : tm_mon =', tt.tm_mon)
print(' : tm_mday =', tt.tm_mday)
print(' : tm_hour =', tt.tm_hour)
print(' : tm_min =', tt.tm_min)
print(' : tm_sec =', tt.tm_sec)
print(' : tm_wday =', tt.tm_wday)
print(' : tm_yday =', tt.tm_yday)
print(' : tm_isdst=', tt.tm_isdst)
print('ordinal:', today.toordinal())
print('Year :', today.year)
print('Mon :', today.month)
print('Day :', today.day)
This example prints the current date in several formats.
D:\Python39\python.exe D:/My_Project/date_time_demo.py
Today is 2023-04-11
ctime : Tue Apr 11 00:00:00 2023
tuple : tm_year = 2023
: tm_mon = 4
: tm_mday = 11
: tm_hour = 0
: tm_min = 0
: tm_sec = 0
: tm_wday = 1
: tm_yday = 101
: tm_isdst= -1
ordinal: 738621
Year : 2023
Mon : 4
Day : 11
Process finished with exit code 0
When I see the program’s result, I suddenly find that one hundred days have already passed. Where have the time gone? So, Please don’t waste time.