datetime
-处理时间和日期的标准库
时间戳:从1970年1月1日0时0分0秒,到当前时间的秒数,浮点数,到毫秒
模块:
-date 日期对象, 常用的属性 year,month,day
-time 时间对象, hour, minute, second, microsecond
-datetime 日期时间对象
-timedelta 时间间隔,两个时间之间的长度
"""
创建 datetime 中的常用对象
"""
#基本用法
from datetime import time,date,datetime,timedelta
d = date(2018,5,6)
print(d,type(d))
t = time(hour=22,minute=34,second=23,microsecond=4567)
print(t,type(t))
dt = datetime(2012,2,22,23,5)
print(dt,type(dt))
td = timedelta(hours=33443.4)
print(td,type(td))
"""
datetime.datetime日期时间对象的常用方法
"""
now = datetime.now()
print(now,type(now))
time_str = now.strftime('%Y-%m-%d,%H:%M:%S,%A %B')
print(time_str,type(time_str))
# dt = datetime.strptime('2018/11/13', '%Y/%m/%d')
# print(dt, type(dt))
print(now.timestamp()) # 浮点数
# 时间戳-》datetime
dt1 = datetime.fromtimestamp(1542373647.00759)
print(dt1, type(dt1))
# date time datetime 都是 不可变对象
print({now: 'xinlan'})
"""
时间格式化(记忆)
%Y Year with century as a decimal number. 2018 18
%m Month as a decimal number [01,12].
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%M Minute as a decimal number [00,59].
%S Second as a decimal number [00,61].
%z Time zone offset from UTC.
%a Locale's abbreviated weekday name. 星期的简写
%A Locale's full weekday name. 星期的全称
%b Locale's abbreviated month name. 月的简写
%B Locale's full month name. 月的全写
%c Locale's appropriate date and time representation.
# Fri Nov 16 21:18:16 2018
%I Hour (12-hour clock) as a decimal number [01,12].
%p Locale's equivalent of either AM or PM.
"""
'''
时间运算
'''
today = datetime.now().date()
print(today,type(today))
tomorrow = today +timedelta(days=1)
print(tomorrow,type(tomorrow))
运行结果:
注意时间是不可变对象,tuple表示。