来源 https://www.cnblogs.com/cindy-cindy/p/6720196.html
https://www.cnblogs.com/sunshineyang/p/6818834.html
https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431937554888869fb52b812243dda6103214cd61d0c2000 侵删
认为原作者写的都很详细,小白总结了一些对自己有用的方法,日后可能会接着完善
总结带有个人的原因,不清楚的可移驾来源查看
重点:格式化strftime(format),timedelta类。datetime和timestamp的转换,str与datetime的转换
ps: timestamp:把1970年1月1日 00:00:00 UTC+00:00时区的时刻称为epoch time,记为0(1970年以前的时间timestamp为负数),当前时间就是相对于epoch time的秒数,称为timestamp。是一个浮点数,没有时区的概念。
datetime 的 5个类:
1. datetime.date 表示日期
datetime.date(year,month,day) 这是它的三个参数
1.1 新建对象:传递三个参数即可
1.1 获得当前日期 datetime.date.today()
1.2 参数传递 a=datetime.date(2018,3,5)
1.3 格式化时间 strftime(format) 注意大小写和百分号,格式为:%Y %m %d %H %M %S
>>> a=datetime.datetime.now()
>>> a
datetime.datetime(2018, 3, 15, 20, 20, 54, 593150)
>>> a.strftime('%Y_%m_%d__%H_%M_%S')
'2018_03_15__20_20_54'
1.4 日期的年月日替换 date.replace() 返回一个替代后的日期,原来的日期不变,与字符串相同
>>> a=datetime.date.today()
>>> a.replace(year=233,month=2,day=3)#年月日不能超出常规范围,会报错range错误i
datetime.date(233, 2, 3)
2.datetime.time 时间
datetime.time(hour,minute,second,microsecond,tzoninfo)
与date类类似 :新建对象, 格式化输出,replace
3.datetime.datetime 日期时间
datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])
3.1 新建对象:传入指定参数即可
3.2 获取当前日期时间 datetime.datetime.today()
3.3 获取指定时区时间 datetime.datetime.now([tz]) 参数为空返回与datetime.datetime.today()相同或者参数是
a tzinfo subclass 对象
3.4 datetime.datetime.strftime(format) 格式化
3.5 替换 datetime.datetime.replace(year,month...)
3.6 datetime与timestamp的互相转换:
datetime-timestamp: timestamp()
>>> a=datetime.datetime.now()
>>> a
datetime.datetime(2018, 3, 18, 21, 22, 29, 696701)
>>> a.timestamp()
1521379349.696701
timestamp->datetime
timestamp->datetime:fromtimestamp()#因为datetime是有时区概念的,所以上述转换实际上是和本地时间转换
如果要转换为标准UTC时间,那么应该是utcfromtimestamp()
>>> datetime.datetime.fromtimestamp(datetime.datetime.now().timestamp())
datetime.datetime(2018, 3, 18, 21, 28, 44, 801701)
3.7 str 与 datetime的转换
str->datetime: strptime()#转换后datetime无时区信息
>>> datetime.datetime.strptime('2018_3_18-21:36:00','%Y_%m_%d-%H:%M:%S')
datetime.datetime(2018, 3, 18, 21, 36)
datetime->str: strftime()
>>> a.strftime('%Y_%m_%d-%H:%M:%S')#这里的a为上文的本地时间
'2018_03_18-21:22:29'
>>> a.strftime('%a,%b,%d %H:%M')#参考廖大大,这里的a代表星期几,b代表月份
'Sun,Mar,18 21:22'
4.datetime.timedelta 计算两个datetime类实例时间间隔,返回仍然是daetime类
>>> a=datetime.datetime.now()
>>> b=datetime.datetime.now()
>>> b-a
datetime.timedelta(0, 9, 523000)
>>> a+datetime.timedelta(days=2)#这里有个s
datetime.datetime(2018, 3, 20, 21, 22, 29, 696701)
5.datetime.tzinfo 时区
1.时区设置
>>> from datetime import datetime,timezone,timedelta
>>> a=timezone(timedelta(hours=8))
>>> b=datetime.now()
>>> b
datetime.datetime(2018, 3, 18, 21, 54, 21, 412701)
>>> b.replace(tzinfo=a)
datetime.datetime(2018, 3, 18, 21, 54, 21, 412701, tzinfo=datetime.timezone(datetime.timedelta(0, 28800)))
2.时区转换
# 拿到UTC时间,并强制设置时区为UTC+0:00:
>>> utc_dt = datetime.utcnow().replace(tzinfo=timezone.utc)
>>> print(utc_dt)
2015-05-18 09:05:12.377316+00:00
#astimezone()将转换时区为北京时间:
>>> bj_dt = utc_dt.astimezone(timezone(timedelta(hours=8)))
>>> print(bj_dt)
2015-05-18 17:05:12.377316+08:00
# astimezone()将转换时区为东京时间:
>>> tokyo_dt = utc_dt.astimezone(timezone(timedelta(hours=9)))
>>> print(tokyo_dt)
2015-05-18 18:05:12.377316+09:00
# astimezone()将bj_dt转换时区为东京时间:
>>> tokyo_dt2 = bj_dt.astimezone(timezone(timedelta(hours=9)))
>>> print(tokyo_dt2)
2015-05-18 18:05:12.377316+09:00