文章目录
datetime
安装datetime库
pip install datetime
获取当前日期
from datetime import datetime
today = datetime.today()
print(today)
#datetime.datetime(2018, 9, 8, 22, 32, 46)
转换输出的时间格式
today.strftime('%Y-%m-%d')
#'2018-09-08'
today.strftime('%Y%m%d')
#'20180908'
today.strftime('%Y-%m')
#'2018-09'
时间日期格式化符号
%y 两位数的年份表示(00-99)
%Y 四位数的年份表示(000-9999)
%m 月份(01-12)
%d 月内中的一天(0-31)
%H 24小时制小时数(0-23)
%I 12小时制小时数(01-12)
%M 分钟数(00=59)
%S 秒(00-59)
%a 本地简化星期名称
%A 本地完整星期名称
%b 本地简化的月份名称
%B 本地完整的月份名称
%c 本地相应的日期表示和时间表示
%j 年内的一天(001-366)
%p 本地A.M.或P.M.的等价符
%U 一年中的星期数(00-53)星期天为星期的开始
%w 星期(0-6),星期天为星期的开始
%W 一年中的星期数(00-53)星期一为星期的开始
%x 本地相应的日期表示
%X 本地相应的时间表示
%Z 当前时区的名称
字符转为时间
datetime.strptime('2018-09-08','%Y-%m-%d') # 方法一
#datetime.datetime(2018, 9, 8, 0, 0)
# strptime函数接受两个参数,第一个是要转换的字符串日期,第二个是日期时间的格式化形式。
import pandas as pd # 方法二,借助pandas模块
pd.to_datetime('2018-09-08')
#Timestamp('2018-09-08 00:00:00')
pd.to_datetime('201909',format='%Y%m')
#Timestamp('2019-09-01 00:00:00')
# to_datetime函数常用的参数有两个,第一个是要转换的字符日期时间格式,第二个是该字符日期时间的格式化形式。
# 对于简单的时间格式第二个参数format可以省略,to_datetime会自动识别,复杂一点的时间格式需要指定format。
数值转换日期/数字转换日期
dt = 20180908
datetime.strptime(str(dt),'%Y%m%d')
#datetime.datetime(2018, 9, 8, 0, 0)
arrow
安装arrow库
pip install arrow
使用arrow库
获取当前时间 arrow.utcnow(), arrow.now()
import arrow
utc = arrow.utcnow() # 获取世界标准时间
<Arrow [2018-06-07T09:37:28.989983+00:00]>
utc = arrow.now() # 获取当前本地时间
<Arrow [2018-06-07T17:40:19.019529+08:00]>
arrow.now('US/Pacific') # 获取指定时区的时间
<Arrow [2018-06-07T02:41:54.815029-07:00]>
将时间转化为时间戳
t = arrow.utcnow()
t.timestamp
1485937837 # 输出
将时间戳转化为arro
# 时间戳可以是int,float或者可以转化为float的字符串
arrow.get(1519534533)
<Arrow [2018-02-25T04:55:33+00:00]>
arrow.get('1519534533')
<Arrow [2018-02-25T04:55:33+00:00]>
arrow.get(1519534533.153443)
<Arrow [2018-02-25T04:55:33.153443+00:00]>
arrow.get('1519534533.153443')
<Arrow [2018-02-25T04:55:33.153443+00:00]>
将时间转化成时间字符串
t = arrow.now()
t.format()
u'2017-02-01 17:00:42+08:00' # 输出
t.format("YYYY-MM-DD HH:mm")
u'2017-02-01 17:00' # 输出
将字符串转化成arrow
arrow.get('2018-02-24 12:30:45', 'YYYY-MM-DD HH:mm:ss')
<Arrow [2018-02-24T12:30:45+00:00]> # 输出
# 遵循ISO-8601的字符串不需要格式字符串参数即可转换
arrow.get('2018-02-24T13:00:00.000-07:00')
<Arrow [2018-02-24T13:00:00-07:00]> # 输出
# 可以从字符串中通过格式参数搜索时间
arrow.get('June was born in May 1980', 'MMMM YYYY')
<Arrow [1980-05-01T00:00:00+00:00]> # 输出
直接创建/生成arrow对象
arrow.Arrow(2018, 2, 24) # 方法一
<Arrow [2018-02-24T00:00:00+00:00]> # 输出
arrow.get(2018, 2, 24) # 方法二
<Arrow [2018-02-24T00:00:00+00:00]> # 输出
arrow对象属性datetime,timestamp,native,tzinfo
a = arrow.utcnow()
a.datetime
datetime.datetime(2018, 2, 24, 21, 15, 50, 841056, tzinfo=tzlocal()) # 输出
a.timestamp
1519478150 # 输出
a.naive
datetime.datetime(2018, 2, 24, 21, 58, 4, 309575) # 输出
a.tzinfo
tzlocal() # 输出
获取datetime对象的值
a = arrow.now()
<Arrow [2018-06-07T17:44:43.519166+08:00]> # 输出
a.year # 当前年
2018 # 输出
a.month # 当前月份
6 # 输出
a.day # 当前天
7 # 输出
a.hour # 当前第几个小时
17 # 输出
a.minute # 当前多少分钟
44 # 输出
a.second # 当前多少秒
43 # 输出
a.timestamp # 获取时间戳
1528364683 # 输出
a.float_timestamp # 浮点数时间戳
1528364683.519166 # 输出
按名称或tzinfo转换为时区
arw = arrow.utcnow()
<Arrow [2018-06-07T11:16:51.695083+00:00]> # 输出
arw.to('US/Pacific')
<Arrow [2018-06-07T04:16:51.695083-07:00]> # 输出
时间推移a.shift(**kwargs)
# 数值为正,向后数; 数值为负,向前数。
a.shift(weeks=+3) #三周后
<Arrow [2018-03-17T21:58:04.309575+08:00]> # 输出
a.shift(days=-1) #一天前
<Arrow [2018-02-23T21:58:04.309575+08:00]> # 输出
a.shift(weekday=6) #距离最近a的星期日,weekday从0到6
<Arrow [2018-02-25T21:58:04.309575+08:00]> # 输出
时间替换–24小时转12小时制
a
<Arrow [2018-02-24T21:58:04.309575+08:00]>
a.replace(hour=9)
<Arrow [2018-02-24T09:58:04.309575+08:00]>
格式化输出
a = arrow.now()
<Arrow [2018-06-07T17:59:36.917894+08:00]> # 输出
a.format()
'2018-06-07 17:59:36+08:00' # 输出
a.format('YYYY-MM-DD HH:mm:ss ZZ')
'2018-06-07 17:59:36 +08:00' # 输出
a.ctime() # 返回日期和时间的ctime格式化表示。
'Thu Jun 7 17:59:36 2018' # 输出
a.weekday() # 以整数形式返回星期几(0-6)
3 # 输出
a.isoweekday() # 以整数形式返回一周中的ISO日(1-7)
4 # 输出
a.isocalendar() # 返回3元组(ISO年,ISO周数,ISO工作日)
(2018, 23, 4) # 输出
a.toordinal() # 返回日期的格雷戈里序数
736852 # 输出
格式化字符串标记
本页所有内容均取自互联网,可参考链接:
datetime参考: https://blog.csdn.net/sinat_30715661/article/details/82534033
arrow参考1: https://www.cnblogs.com/lincappu/p/14657195.html
arrow参考2: https://www.jianshu.com/p/c878bb1c48c1