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))
Python 操作time datetime calendar 操作时间、日期以及日历
于 2018-07-11 17:12:45 首次发布