Python 操作time datetime calendar 操作时间、日期以及日历

import time,datetime,calendar

print(datetime.datetime.now(),type(datetime.datetime.now()))    # 获取当前datetime,返回datetime对象
print(datetime.datetime.now().date(),type(datetime.datetime.now().date()))  # 获取当前日期,返回datetime对象
print(str(datetime.datetime.now().date()))  # 将datetime对象转换成日期字符串
print(datetime.datetime.now().time(),type(datetime.datetime.now().time()))  # 获取当前时间,返回datetime对象
print(time.time(),type(time.time()))    # 获取当前时间戳,返回浮点数
print(time.localtime())   # 获取本地当前时间,返回的是一个时间元组
print(time.asctime(time.localtime()),type(time.asctime(time.localtime())))   # 获取一个格式化的时间,返回英文文本时间字符串
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()))   # 格式化输出时间,将时间元组转换成指定的时间字符串
print(time.strptime('2018-12-10 10:20:23',"%Y-%m-%d %H:%M:%S"))  # 将指定格式的时间字符串,转换成时间元组,返回时间元组对象
print(time.mktime(time.strptime('2018-12-10 10:20:23',"%Y-%m-%d %H:%M:%S")))  # 将指定格式的时间字符串,转换成时间元组,然后再转换成时间戳

print(time.localtime(time.time())) #将当前的时间戳转换成本地时间元组
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())) #将本地时间元组转换成指定的时间格式
# 时间字符串转datetime对象
print(datetime.datetime.strptime('2019-05-17', '%Y-%m-%d'))
# 转换时间字符串格式
a = "2013-10-10 23:40:00" #转换为 a = "2013/10/10 23:40:00"
timeArray = time.strptime(a, "%Y-%m-%d %H:%M:%S")
otherStyleTime = time.strftime("%Y/%m/%d %H:%M:%S", timeArray)
print(a,otherStyleTime)
# 时间字符串转换成时间戳
print(time.mktime(time.strptime('2018-12-10 10:20:23',"%Y-%m-%d %H:%M:%S"))) 
# datetime对象转换成时间戳
print(time.mktime(time.strptime(str(datetime.datetime.now()),'%Y-%m-%d %H:%M:%S.%f')))
# datetime 转时间戳
print(time.mktime(datetime.datetime.now().timetuple()))  
# datetime对象转换成时间字符串
print(str(datetime.datetime.now())   # 得到具体日期时间字符串
print(str(datetime.datetime.now().date())  # 得到日期字符串
print(str(datetime.datetime.now().time())  # 得到时间字符串
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))  # 将datetime的当前时间格式化成想要的时间格式

print(datetime.datetime.now()) #使用datetime获取当前日期和时间
print(datetime.datetime.now().date()) #使用datetime获取当前的日期
print(datetime.datetime.now().time()) #使用datetime获取当前的时间
print(datetime.datetime.fromtimestamp(time.time())) #时间戳转换为datetime时间

print(calendar.month(2018,6)) #返回制定的日历
print(calendar.weekday(2018,6,10)) #确定制定日期为周几,从0开始周一为0
print(calendar.isleap(2018)) #确定当前给定年是否为闰年,返回为布尔值
print(calendar.leapdays(2018,2088)) #确定另个年的间隔之间的闰年总数

# datetime 加一天 支持和时间对象做bool运算
print(datetime.datetime.now()+datetime.timedelta(days=1))
# datetime 加一小时
print(datetime.datetime.now()+datetime.timedelta(hours=1))
# datetime 加一分钟
print(datetime.datetime.now()+datetime.timedelta(minutes=1))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

haeasringnar

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值