time,datetime模块
time模块
时间戳
返回1970年1月1日 00:00:00开始按秒计算时间偏移量
time_stamp = time.time()
print(time_stamp,type(time_stamp))
>>>1569671240.0007255 <class 'float'>
格式化时间
format_time = time.strfttime("%Y-%m-%d %X")
print(format_time, type(format_time))
>>>2019-09-28 19:51:09 <class 'str'>
time.strfttime(参数)
%Y Year with century as a decimal number.
%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.
- time.sleep(s) 睡s秒
datetime模块
参数列表
year=1949,
month=10,
day=1,
hour=10,
minute=1,
second=0,
microsecond=0
返回当前时间
now = datetime.datetime.now()
print(now)
更改时间
print(datetime.datetime.now() + datetime.timedelta(3))
默认更改天,可使用参数进行更改
时间替换
print(now.replace(year=1949, month=10, day=1, hour=10, minute=1, second=0, microsecond=0))