最近写接口测试脚本会遇上时间的问题,所以特意搜集了比较常用的,记录一下。
以秒数返回的方式,返回当前的时间戳
print(time.time())
#类型:
1561019311.9252782
最常用的是转换为时间格式
strftime() 方法用于接收时间元组,并返回以可读字符串表示的当地时间。格式由format参数决定。
strftime()只能接受struct_time类型的参数,若提供的是9位元素的时间元组,则需要将其转化为时间戳再转化为struct_time类型的时间元组
1、参数 t – 这是要被格式化以秒为单位的时间,为一个可选参数。
2、参数format – 这将用于格式化给定时间的指令。
3、返回值:返回以可读字符串表示的当地时间
%Y : 表示年
%m(小写):表示月
%d(小写):表示日
%H:表示小时
%M:表示分钟
%S:表示秒
#将当前时间格式化,根据需要可以进行增减所需要显示的内容
timedata = time.strftime('%Y-%m-%d %H:%M:%S')
timedata = time.strftime('%Y/%m/%d')
#类型:
2019-06-20 10:41:33
2019/06/20
datetime模块中有几个类:
datetime.datetime:日期时间类
datetime.date:日期类,常用的属性有year/month/day
datetime.time:时间类,常用的有hour/minute/second/microsecond
datetime.timedelta:时间间隔,即两个时间点之间的时间长度,主要用于时间的加减
datetime.datetime.now()
返回当前时间
print(datetime.datetime.now())
2019-06-20 10:41:33.315383
#类型:
datetime.date.today()
直接输出日期
print(datetime.date.today())
2019-06-20
print(datetime.date.today().year)
2019
print(datetime.date.today().month)
6
print(datetime.date.today().day)
20
#.year/month/day类型:
datetime.date/time.max
.date/time类能表示的最大的年、月、日的数值
datetime.date/time.min
.date/time类能表示的最小的年、月、日的数值
print(datetime.date.max)
print(datetime.date.min)
9999-12-31
0001-01-01
#类型:
print(datetime.time.min)
print(datetime.time.max)
00:00:00
23:59:59.999999
#类型:
主要应用于日期时间的加减
timedata = time.strftime('%Y-%m-%d %H:%M:%S')
print(timedata)
print(type(timedata))
beforeTime=(datetime.datetime.now()+datetime.timedelta(weeks=-1)).strftime("%Y-%m-%d %H:%M:%S")
print(beforeTime)
print(type(beforeTime))
serechData={
"access_token": tokenU,
"jwtauth_auth_ret_type": "json",
"command": "query_trade_pack",
"start_date": beforeTime,
"end_date": timedata,
"dev_list": [dev_id]
}
2019-06-20 16:25:52
2019-06-13 16:25:52
参考:
https://blog.csdn.net/qq_41573234/article/details/82533820
https://blog.csdn.net/wuxiaobingandbob/article/details/46544203