|
@@ -7,9 +7,118 @@
|
|
|
# @Email : zhangdongming@asj6.wecom.work
|
|
|
# @File : LocalDateTimeUtil.py
|
|
|
# @Software: PyCharm
|
|
|
+import calendar
|
|
|
import datetime
|
|
|
import time
|
|
|
|
|
|
+from dateutil.relativedelta import relativedelta
|
|
|
+
|
|
|
+
|
|
|
+def get_cur_month():
|
|
|
+ # 获取当前月
|
|
|
+ return datetime.datetime.now().strftime("%Y-%m")
|
|
|
+
|
|
|
+
|
|
|
+def get_last_month_num(number=1):
|
|
|
+ # 获取前几个月
|
|
|
+ month_date = datetime.datetime.now().date() - relativedelta(months=number)
|
|
|
+ return month_date.strftime("%Y-%m")
|
|
|
+
|
|
|
+
|
|
|
+def get_next_month(number=1):
|
|
|
+ # 获取后几个月
|
|
|
+ month_date = datetime.datetime.now().date() + relativedelta(months=number)
|
|
|
+ return month_date.strftime("%Y-%m")
|
|
|
+
|
|
|
+
|
|
|
+def get_cur_month_start():
|
|
|
+ # 获取当前月的第一天
|
|
|
+ month_str = datetime.datetime.now().strftime('%Y-%m')
|
|
|
+ return '{}-01'.format(month_str)
|
|
|
+
|
|
|
+
|
|
|
+def get_cur_month_end():
|
|
|
+ # 获取当前月的最后一天
|
|
|
+ """
|
|
|
+ param: month_str 月份,2021-04
|
|
|
+ """
|
|
|
+ # return: 格式 %Y-%m-%d
|
|
|
+
|
|
|
+ month_str = datetime.datetime.now().strftime('%Y-%m')
|
|
|
+ year, month = int(month_str.split('-')[0]), int(month_str.split('-')[1])
|
|
|
+ end = calendar.monthrange(year, month)[1]
|
|
|
+ return '{}-{}-{}'.format(year, month, end)
|
|
|
+
|
|
|
+
|
|
|
+def get_last_month_start(month_str=None):
|
|
|
+ # 获取上一个月的第一天
|
|
|
+ """
|
|
|
+ param: month_str 月份,2021-04
|
|
|
+ """
|
|
|
+ # return: 格式 %Y-%m-%d
|
|
|
+ if not month_str:
|
|
|
+ month_str = datetime.datetime.now().strftime('%Y-%m')
|
|
|
+ year, month = int(month_str.split('-')[0]), int(month_str.split('-')[1])
|
|
|
+ if month == 1:
|
|
|
+ year -= 1
|
|
|
+ month = 12
|
|
|
+ else:
|
|
|
+ month -= 1
|
|
|
+ return '{}-{}-01'.format(year, month)
|
|
|
+
|
|
|
+
|
|
|
+def get_next_month_start(month_str=None):
|
|
|
+ # 获取下一个月的第一天
|
|
|
+ """
|
|
|
+ param: month_str 月份,2021-04
|
|
|
+ """
|
|
|
+ # return: 格式 %Y-%m-%d
|
|
|
+ if not month_str:
|
|
|
+ month_str = datetime.datetime.now().strftime('%Y-%m')
|
|
|
+ year, month = int(month_str.split('-')[0]), int(month_str.split('-')[1])
|
|
|
+ if month == 12:
|
|
|
+ year += 1
|
|
|
+ month = 1
|
|
|
+ else:
|
|
|
+ month += 1
|
|
|
+ return '{}-{}-01'.format(year, month)
|
|
|
+
|
|
|
+
|
|
|
+def get_last_month_end(month_str=None):
|
|
|
+ # 获取上一个月的最后一天
|
|
|
+ """
|
|
|
+ param: month_str 月份,2021-04
|
|
|
+ """
|
|
|
+ # return: 格式 %Y-%m-%d
|
|
|
+ if not month_str:
|
|
|
+ month_str = datetime.datetime.now().strftime('%Y-%m')
|
|
|
+ year, month = int(month_str.split('-')[0]), int(month_str.split('-')[1])
|
|
|
+ if month == 1:
|
|
|
+ year -= 1
|
|
|
+ month = 12
|
|
|
+ else:
|
|
|
+ month -= 1
|
|
|
+ end = calendar.monthrange(year, month)[1]
|
|
|
+ return '{}-{}-{}'.format(year, month, end)
|
|
|
+
|
|
|
+
|
|
|
+def get_next_month_end(month_str=None):
|
|
|
+ # 获取下一个月的最后一天
|
|
|
+ """
|
|
|
+ param: month_str 月份,2021-04
|
|
|
+ """
|
|
|
+ # return: 格式 %Y-%m-%d
|
|
|
+ if not month_str:
|
|
|
+ month_str = datetime.datetime.now().strftime('%Y-%m')
|
|
|
+ year, month = int(month_str.split('-')[0]), int(month_str.split('-')[1])
|
|
|
+ if month == 12:
|
|
|
+ year += 1
|
|
|
+ month = 1
|
|
|
+ else:
|
|
|
+ month += 1
|
|
|
+ end = calendar.monthrange(year, month)[1]
|
|
|
+ return '{}-{}-{}'.format(year, month, end)
|
|
|
+
|
|
|
|
|
|
def get_last_first_date_and_last_date(n):
|
|
|
"""
|
|
@@ -43,6 +152,15 @@ def get_today_date(timestamp=False):
|
|
|
return zero_today, last_today
|
|
|
return zero_today, last_today
|
|
|
|
|
|
+def get_last_week():
|
|
|
+ """
|
|
|
+ 获取前一周时间
|
|
|
+ @return: last_week_date
|
|
|
+ """
|
|
|
+ today = datetime.date.today() # 1. 获取「今天」
|
|
|
+ last_week = today - datetime.timedelta(days=7) # 2.获取7天前
|
|
|
+ last_week_date = last_week.strftime("%Y-%m-%d %H:%M:%S")
|
|
|
+ return last_week_date
|
|
|
|
|
|
def get_last_month():
|
|
|
"""
|
|
@@ -71,6 +189,21 @@ def get_before_days_timestamp(timestamp, days=1):
|
|
|
return 0
|
|
|
|
|
|
|
|
|
+def get_after_days_timestamp(timestamp, days=1):
|
|
|
+ """
|
|
|
+ 获取之后日期时间戳-秒级
|
|
|
+ @param timestamp: 时间戳
|
|
|
+ @param days: 天数
|
|
|
+ @return: (timestamp + second * hour * days) 时间戳
|
|
|
+ """
|
|
|
+ if days:
|
|
|
+ second = 3600
|
|
|
+ hour = 24
|
|
|
+ if days > 0:
|
|
|
+ return timestamp + second * hour * days
|
|
|
+ return 0
|
|
|
+
|
|
|
+
|
|
|
def date_to_week(str_date):
|
|
|
"""
|
|
|
日期获取星期几
|
|
@@ -110,10 +243,43 @@ def get_start_and_end_time(date, str_format):
|
|
|
return 0
|
|
|
|
|
|
|
|
|
-if __name__ == "__main__":
|
|
|
+if __name__ == '__main__':
|
|
|
+ zero_today, last_today = get_today_date(True)
|
|
|
+ month_end = get_cur_month_end()
|
|
|
+ start_time, month_end_time = get_start_and_end_time(month_end, '%Y-%m-%d')
|
|
|
+ print(zero_today)
|
|
|
+ print(month_end_time)
|
|
|
+
|
|
|
+
|
|
|
+ # # 获取当前月
|
|
|
+ # print('当前月', get_cur_month())
|
|
|
+ # # 获取上一个月
|
|
|
+ # print('上一个月', get_last_month_num())
|
|
|
+ # # 获取上两个月
|
|
|
+ # print('上两个月', get_last_month_num(number=2))
|
|
|
+ # # 获取下一个月
|
|
|
+ # print('下一个月', get_next_month())
|
|
|
+ # # 获取下两个月
|
|
|
+ # print('下两个月', get_next_month(number=2))
|
|
|
+ # # 获取当前月的第一天
|
|
|
+ # print('当前月的第一天', get_cur_month_start())
|
|
|
+ # # 获取当前月的最后一天
|
|
|
+ # print('当前月的最后一天', get_cur_month_end())
|
|
|
+ # # 获取上个月的第一天
|
|
|
+ # print('上个月的第一天', get_last_month_start())
|
|
|
+ # # 获取下个月的第一天
|
|
|
+ # print('下个月的第一天', get_next_month_start())
|
|
|
+ # # 获取上个月的最后一天
|
|
|
+ # print('上个月的最后一天', get_last_month_end())
|
|
|
+ # # 获取下个月的最后一天
|
|
|
+ # print('下个月的最后一天', get_next_month_end())
|
|
|
dd = str(1650791368303)
|
|
|
print(dd[0:10])
|
|
|
print(dd[10:])
|
|
|
dateArray = datetime.datetime.utcfromtimestamp(1650791368)
|
|
|
print(dateArray.date())
|
|
|
- print(get_start_and_end_time('20220317', '%Y%m%d'))
|
|
|
+ next_start_time, next_end_time = get_start_and_end_time(get_next_month_start(), '%Y-%m-%d')
|
|
|
+ print(type(next_end_time))
|
|
|
+ print('下月开始时间{}'.format(next_start_time))
|
|
|
+ start_time, end_time = get_start_and_end_time(get_next_month_end(), '%Y-%m-%d')
|
|
|
+ print('下月结束时间{}'.format(end_time))
|