python time模块和calendar模块

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月的日历

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值