# 根据字符串类型转日期 返回值类型
st_time = time.strptime('2019-4-30 11:32:23', '%Y-%m-%d %H:%M:%S')
print(str(st_time) + " 类型:" + str(type(st_time)))
# time.struct_time(tm_year=2019, tm_mon=4, tm_mday=30, tm_hour=11, tm_min=32, tm_sec=23, tm_wday=1, tm_yday=120, tm_isdst=-1) 类型:
# 根据日期类型转字符 返回值类型
str_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
print(str_time + " 类型:" + str(type(str_time)))
# 2019-04-26 12:33:04 类型:
# 获取当前日期 返回值类型
st_time = time.localtime()
print(str(st_time) + " 类型:" + str(type(st_time)))
# time.struct_time(tm_year=2019, tm_mon=4, tm_mday=26, tm_hour=12, tm_min=47, tm_sec=41, tm_wday=4, tm_yday=116, tm_isdst=0) 类型:
# 获取当前日期时间戳 返回值类型
float_time = time.time()
print(str(float_time) + " 类型:" + str(type(float_time)))
# 1556254126.6432147 类型:
# 根据日期类型转换为时间戳 返回值类型
float_time = time.mktime(time.localtime())
print(str(float_time) + " 类型:" + str(type(float_time)))
# 1556256409.0 类型:
# 根据日期戳转换为struct_time日期 返回值类型
st_time = time.localtime(time.time())
print(str(st_time) + " 类型:" + str(type(st_time)))
# time.struct_time(tm_year=2019, tm_mon=4, tm_mday=26, tm_hour=15, tm_min=5, tm_sec=48, tm_wday=4, tm_yday=116, tm_isdst=0) 类型:
relativedelta 类型旨在应用于现有日期时间,并且可以替换该日期时间的特定组件,或表示时间间隔。
使用简单:
eg:relativedelta(datetime1, datetime2)
relativedelta(arg1=x,arg2=y,arg3=z...)
year, month, day, hour, minute, second, microsecond:
Absolute information (argument is singular); adding or subtracting a
relativedelta with absolute information does not perform an arithmetic
operation, but rather REPLACES the corresponding value in the
original datetime with the value(s) in relativedelta.
years, months, weeks, days, hours, minutes, seconds, microseconds:
Relative information, may be negative (argument is plural); adding
or subtracting a relativedelta with relative information performs
the corresponding arithmetic operation on the original datetime value
with the information in the relativedelta.
weekday:
One of the weekday instances (MO, TU, etc) available in the
relativedelta module. These instances may receive a parameter N,
specifying the Nth weekday, which could be positive or negative
(like MO(+1) or MO(-2)). Not specifying it is the same as specifying
+1. You can also use an integer, where 0=MO. This argument is always
relative e.g. if the calculated date is already Monday, using MO(1)
or MO(-1) won't change the day. To effectively make it absolute, use
it in combination with the day argument (e.g. day=1, MO(1) for first
Monday of the month).
leapdays:
Will add given days to the date found, if year is a leap
year, and the date found is post 28 of february.
yearday, nlyearday:
Set the yearday or the non-leap year day (jump leap days).
These are converted to day/month/leapdays information.
关键字参数有相对形式和绝对形式。复数是相对的,单数是绝对的。对于以下顺序中的每个参数,首先应用绝对形式(通过将每个属性设置为该值),然后应用相对形式(通过将值添加到属性)。
将此 relativedelta 添加到日期时间时考虑的属性顺序为:
年
月
天
小时
分钟
秒
微秒
最后,使用上述规则应用工作日。
例如:
>>> from datetime import datetime
>>> from dateutil.relativedelta import relativedelta, MO
>>> dt = datetime(2018, 4, 9, 13, 37, 0)
>>> delta = relativedelta(hours=25, day=1, weekday=MO(1))
>>> dt + delta
datetime.datetime(2018, 4, 2, 14, 37)
使用示例:
date_end = date_start.date() + relativedelta(months=3, days=-1)
返回一个dateutil.relativedelta.relativedelta对象。
构造函数:datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
其中参数都是可选,默认值为0
其中:
1 millisecond = 1000 microseconds
1 minute = 60 seconds
1 hour = 3600 seconds
1 week = 7 days
在构造函数中,注意参数值的范围:
0 <= microseconds < 1000000
0 <= seconds < 3600*24 (the number of seconds in one day)
-999999999 <= days <= 999999999
import datetime
nowtime=datetime.datetime.now()
print(nowtime.strftime('%Y-%m-%d %H:%M:%S'))
delta=datetime.timedelta(days=1)
rs=nowtime+delta
print(rs.strftime('%Y-%m-%d %H:%M:%S'))