import time,datetime
# 当前时间
now_time = datetime.datetime.now()
now_time_mk = time.mktime(now_time.timetuple()) # 当前时间戳
# 下月第一天最后一秒时间
month = now_time.month
if month == 12:
month = 0
future_mouth_first = datetime.datetime(now_time.year, month + 1,1, 23, 59, 59)
# 当月最后一天最后一秒时间
this_month = future_mouth_first - datetime.timedelta(days=1)
# 当月最后一天最后一秒时间戳
this_month_mk = time.mktime(this_month.timetuple())
# 当月第一点最后一秒
first_day = datetime.datetime(now_time.year, now_time.month, 1, 23, 59, 59)
# 上个月最后一天
up_last = first_day - datetime.timedelta(days=1)
# 上个月第一天第一秒
up_first_day = datetime.datetime(up_last.year, up_last.month, 1)
timedalte 是datetime中的一个对象,该对象表示两个时间的差值
构造函数: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
timedalte 有三个只读属性:
timedelta.min:负数最大时间差,相当于 timedelta(-999999999)。
timedelta.max:正数最大时间差,相当于 timedelta(days=999999999, hours=23, minutes=59, seconds=59, microseconds=999999)。
timedelta.resolution:两个时间的最小差值 相当于 timedelta(microseconds=1)。
In [18]: datetime.datetime(now_time.year, now_time.month + 1, 1, 23, 59, 59)
Out[18]: datetime.datetime(2018, 4, 1, 23, 59, 59)
In [19]: datetime.timedelta(1)
Out[19]: datetime.timedelta(1)
In [20]: datetime.datetime(now_time.year, now_time.month + 1, 1, 23, 59, 59) - datetime.time
...: delta(days=1)
Out[20]: datetime.datetime(2018, 3, 31, 23, 59, 59)