datetime.date 类是表示具有年、月、日属性的类。使用 today()类方法可返回当前日期的 date 对象。例如:
import datetime today = datetime.date.today() print(today) print(today.year, today.month, today.day)
输出:
2021-11-28 2021 11 28
可以利用 date 类的 ctime()方法返回一个日期的字符串表示。例如:
print('ctime :', today.ctime())
输出:
ctime : Sun Nov 28 00:00:00 2021
可以使用 date 类的 timetuple ()方法返回一个 time.struct_time 对象。例如:
tt = today.timetuple() print(tt) print('{:^9} {:^9} {:^9} {:^9} {:^9} {:^9}{:^9} {:^9} {:^9}'.format('tm_year','tm_mon','tm_mday','tm_hour','tm_min','tm_sec','tm_wday','tm_yday','tm_isdst')) print('{:^9} {:^9} {:^9} {:^9} {:^9} {:^9}{:^9} {:^9} {:^9}'.format(tt.tm_year,tt.tm_mon,tt.tm_mday,tt.tm_hour,tt.tm_min,tt.tm_sec,tt.tm_wday, tt.tm_yday,tt.tm_isdst))
输出:
time.struct_time(tm_year=2021, tm_mon=11, tm_mday=28, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=332, tm_isdst=-1) tm_year tm_mon tm_mday tm_hour tm_min tm_sec tm_wday tm_yday tm_isdst 2021 11 28 0 0 0 6 332 -1