def paserTime(timestamp):
t = time.time()
f=time.localtime(timestamp/1000)
print (t) #原始时间数据
# print (int(t)) #秒级时间戳
print (int(round(t * 1000))) #毫秒级时间戳
#nowTime = lambda: int(round(t * 1000))
# print(nowTime()); # 毫秒级时间戳,基于lambda
nowTime = lambda:timestamp
str=time.strftime('%Y-%m-%d %H:%M:%S',f)
print(str) # 日期格式化
return str
# paserTime(time.time())
def now2():
nowTime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') # 现在
print(nowTime)
def nowTime():
t = time.time()
f = time.localtime(int(t))
# print(t)
str = time.strftime('%Y-%m-%d %H:%M:%S', f)
print(str) # 日期格式化
return str
# nowTime()
def long2Str(longTime):
f = time.localtime(int(longTime))
# print(t)
str = time.strftime('%Y-%m-%d %H:%M:%S', f)
print(str) # 日期格式化
return str
def str2time(str):
date_time = datetime.datetime.strptime(str, '%Y-%m-%d %H:%M:%S')
# print(date_time)
return date_time
def str2timestamp(str):
timstamp=time.mktime(time.strptime(str, '%Y-%m-%d %H:%M:%S'))
# print(timstamp)
return timstamp
# time.mktime() 与 time.localtime() 互为还原函数。
# time.mktime(timetuple) :将时间元组转换成时间戳
# time.localtime([timestamp]):将时间戳转会为时间元组
# print(str2timestamp(str2)-str2timestamp(str))
def date2datetime(date_param):
date_str = date_param.strftime('%Y-%m-%d %H:%M:%S')
date_time = datetime.datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S')
# print(date_time)
return date_time
def month():
first_day=datetime.date.today().replace(day=1).strftime('%Y-%m-%d %H:%M:%S')
print(first_day)
_end_time = last_day_of_month(datetime.date.today()).__str__() + ' ' + '23:59:59'
print(_end_time)
def get_current_week():
monday, sunday = datetime.date.today(), datetime.date.today()
one_day = datetime.timedelta(days=1)
while monday.weekday() != 0:
monday -= one_day
while sunday.weekday() != 6:
sunday += one_day
monday_datetime = date2datetime(monday)
sunday_datetime = date2datetime(sunday)
print(monday_datetime)
return monday_datetime, sunday_datetime
# 返回时间字符串
# return datetime.datetime.strftime(monday, "%Y/%m/%d") + ' 00:00:00+08:00', datetime.datetime.strftime(sunday, "%Y/%m/%d")+ ' 23:59:59+08:00'
def last_day_of_month(any_day):
"""
获取获得一个月中的最后一天
:param any_day: 任意日期
:return: string
"""
next_month = any_day.replace(day=28) + datetime.timedelta(days=4) # this will never fail
return next_month - datetime.timedelta(days=next_month.day)
# 上月第一天和最后一天
def last_month():
this_month_start = datetime.datetime(now.year, now.month, 1)
last_month_end = this_month_start - timedelta(days=1) + datetime.timedelta(
hours=23, minutes=59, seconds=59)
last_month_start = datetime.datetime(last_month_end.year, last_month_end.month, 1)
print('---last_month_start = {} last_month_end = {}'.format(last_month_start,last_month_end))
return last_month_start,last_month_end
# 本月第一天和最后一天
def this_month():
this_month_start = datetime.datetime(now.year, now.month, 1)
# this_month_end = datetime.datetime(now.year, now.month + 1, 1) - timedelta(days=1) + datetime.timedelta(
# hours=23, minutes=59, seconds=59)
next_month = now.replace(day=28) + datetime.timedelta(days=4)
this_month_end = next_month - datetime.timedelta(days=next_month.day)+ datetime.timedelta(
hours=23, minutes=59, seconds=59)
print('--- this_month_start = {} this_month_end = {}'.format(this_month_start, this_month_end))
return this_month_start,this_month_end
# 上周第一天和最后一天
def last_week():
last_week_start = now - timedelta(days=now.weekday() + 7)
last_week_end = now - timedelta(days=now.weekday() + 1)+ datetime.timedelta(
hours=23, minutes=59, seconds=59)
print('--- last_week_start = {} last_week_end = {}'.format(last_week_start, last_week_end))
return last_week_start,last_week_end
# 本周第一天和最后一天
def this_week():
this_week_start = now - timedelta(days=now.weekday())
this_week_end = now + timedelta(days=6 - now.weekday())+ datetime.timedelta(
hours=23, minutes=59, seconds=59)
print('--- this_week_start = {} this_week_end = {}'.format(this_week_start, this_week_end))
return this_week_start, this_week_end
if __name__=='__main__':
str='2017-02-28 03:40:10'
now_time = str2time(str)
# now_time = datetime.datetime.today()
now =datetime.datetime(now_time.year, now_time.month, now_time.day)
# longTime=str2timestamp(str)+60*2
# long2Str(longTime)
# now()
# month()
# month2()
# print(get_current_week())
print(last_day_of_month(now))
last_month()
this_month()
last_week()
this_week()```
![](https://mutouzuo.oss-cn-hangzhou.aliyuncs.com/my/mudouzuo1.png)