Python 操作time datetime calendar 操作时间、日期已经日历

import time,datetime,calendar
mydatetime= '2018-06-06T14:00:00'
print(time.time()) #获取当前时间的时间戳
print(time.mktime(time.localtime())) #获取本地时间的时间戳
print(time.strptime(mydatetime,'%Y-%m-%dT%H:%M:%S')) #将字符串转化成时间元组
times = time.mktime(time.strptime(mydatetime,'%Y-%m-%dT%H:%M:%S')) #将指定的字符串时间格式转换成时间戳

print(time.localtime(time.time())) #将当前的时间戳转换成本地时间元组
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())) #将本地时间元组转换成指定的时间格式

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(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)) #确定另个年的间隔之间的闰年总数

猜你喜欢

转载自blog.csdn.net/haeasringnar/article/details/81003875