time 模块主要包含了各种提供日期、。时间功能的类和函数,该模块既提供了把日期、时间格式转化为字符串的功能,也提供了从字符串恢复日期、时间的功能。
time.asctime():将时间元组或struct_time转换为时间字符串,如果不指定参数,则默认转换当前时间
time.ctime():将以秒数为代表的时间转换为时间字符串
time.time():返回从1970年1月1日0点整到现在过了多少秒
time.strftime():将时间元组或struct_time对象格式化为指定格式的时间字符串,如果不指定参数,则默认转换当前时间。
关于calendar模块,常用方法见代码:
import calendar
import datetime
import time
print(time.time()) # 获取从1970-01-01 00:00:00 UTC 到现在时间的秒数
print(time.strftime("%Y-%m-%d %H:%M:%S")) # 按照指定格式输出时间
print(time.asctime()) # Mon Apr 15 20:03:23 2019
print(time.ctime(1567404000.6908512)) # Mon Apr 15 20:03:23 2019
print('hello')
# time.sleep(10) # 让线程暂停10秒钟
print('world')
print(datetime.datetime.now()) # 2019-04-15 20:09:05.190113
print(datetime.datetime.now().replace(year=2020)) # 2020-04-15 20:12:37.832657
print(datetime.datetime.now() + datetime.timedelta(weeks=1)) # 2019-04-22 20:11:56.399726
calendar.setfirstweekday(calendar.MONDAY) # 设置每周起始日期码。周一到周日分别对应 0 ~ 6
calendar.firstweekday() # 返回当前每周起始日期的设置。默认情况下,首次载入calendar模块时返回0,即星期一。
c = calendar.calendar(2019) # 生成2019年的日历,并且以周日为其实日期码
print(c) # 打印2019年日历
print(calendar.isleap(2000)) # True.闰年返回True,否则返回False
count = calendar.leapdays(1996, 2010) # 获取1996年到2010年一共有多少个闰年
print(calendar.month(2019, 9)) # 打印2019年3月的日历