| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 | 
							- #!/usr/bin/python3.6
 
- # -*- coding: utf-8 -*-
 
- #
 
- # Copyright (C) 2022 #
 
- # @Time    : 2022/3/26 16:20
 
- # @Author  : ming
 
- # @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):
 
-     """
 
-     获取前n周开始时间和结束时间,参数n:代表前n周
 
-     """
 
-     now = datetime.datetime.now()
 
-     # 上周第一天和最后一天
 
-     before_n_week_start = now - datetime.timedelta(days=now.weekday() + 7 * n, hours=now.hour, minutes=now.minute,
 
-                                                    seconds=now.second, microseconds=now.microsecond)
 
-     # last_week_end = now - timedelta(days=now.weekday() + 1)
 
-     before_n_week_end = before_n_week_start + datetime.timedelta(days=6, hours=23, minutes=59, seconds=59)
 
-     return before_n_week_start, before_n_week_end
 
- def get_today_date(timestamp=False):
 
-     """
 
-     返回当天开始时间和结束时间
 
-     Args:
 
-         timestamp 是否返回时间戳
 
-     returns:
 
-         zero_today ,last_today
 
-     """
 
-     now = datetime.datetime.now()
 
-     zero_today = now - datetime.timedelta(hours=now.hour, minutes=now.minute, seconds=now.second,
 
-                                           microseconds=now.microsecond)
 
-     last_today = zero_today + datetime.timedelta(hours=23, minutes=59, seconds=59)
 
-     if timestamp:
 
-         zero_today = int(time.mktime(zero_today.timetuple()))
 
-         last_today = int(time.mktime(last_today.timetuple()))
 
-         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():
 
-     """
 
-     获取前一个月时间
 
-     returns:
 
-         last_month_date
 
-     """
 
-     today = datetime.date.today()  # 1. 获取「今天」
 
-     last_month = today.replace(month=today.month - 1)  # 2.获取前一个月
 
-     last_month_date = last_month.strftime("%Y-%m-%d %H:%M:%S")
 
-     return last_month_date
 
- def get_before_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 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):
 
-     """
 
-     日期获取星期几
 
-     @param str_date 日期 例:2022-03-03
 
-     @return: int 1-7
 
-     """
 
-     if str_date:
 
-         return datetime.datetime.strptime(str_date, "%Y-%m-%d").weekday() + 1
 
-     return datetime.datetime.now().weekday() + 1
 
- def format_date_to_week(str_date, str_format):
 
-     """
 
-     日期获取星期几
 
-     @param str_format:  %Y-%m-%d
 
-     @param str_date 日期 例:2022-03-03
 
-     @return: int 1-7
 
-     """
 
-     if str_date:
 
-         return datetime.datetime.strptime(str_date, str_format).weekday() + 1
 
-     return 0
 
- def get_start_and_end_time(date, str_format):
 
-     """
 
-     根据日期获取当日开始and结束时间戳
 
-     @param date: 日期 例:2022-03-07
 
-     @param str_format: 格式 例:%Y-%m-%d
 
-     @return: start_time,end_time
 
-     """
 
-     if date:
 
-         today = datetime.datetime.strptime(date, str_format)
 
-         day = today + datetime.timedelta(days=1)
 
-         start_time = int(time.mktime(time.strptime(str(today.date()), '%Y-%m-%d')))
 
-         end_time = int(time.mktime(time.strptime(str(day.date()), '%Y-%m-%d'))) - 1
 
-         return start_time, end_time
 
-     return 0
 
- 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())
 
-     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))
 
 
  |