"""获取某个时间""" import datetime import time from dateutil import relativedelta # 上月(eg: 202201) last_month = (datetime.datetime.now() + relativedelta.relativedelta(months=-1)).strftime("%Y%m") # 当月(eg: 202202) issue_no = time.strftime("%Y%m") # 下月(eg: 202203) nextmonth = (datetime.date.today() + relativedelta.relativedelta(months=1)).strftime("%Y%m") # 年月日(eg:2022-03-01) now = datetime.datetime.now().strftime('%Y-%m-%d') now = datetime.datetime.now().strftime("%Y%m%d") # 年月日时分秒(eg:2022-03-01 00:00:00) now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') # 当月第一天,下月第一天 month_first_day = datetime.date(year=today.year, month=today.month, day=1) next_clear_day = month_first_day + relativedelta(months=1) # 3天前的日期 threeDayAgo = (datetime.datetime.now() - datetime.timedelta(days = 3)) # 31天后的日期 starttime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') endtime = (datetime.datetime.now() + datetime.timedelta(days=31)).strftime('%Y-%m-%d 00:00:00')
"""时间转换""" import time import datetime # 时间戳转字符串格式时间 timestamp = time.time() # 当前时间戳 strtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timestamp)) # 字符串格式时间转时间戳 now_str = "2022-03-09 00:00:00" now_time = time.strptime(now_str,'%Y-%m-%d %H:%M:%S') timestamp = time.mktime(now_time)
# python日期格式化 week = time.strftime("%A")