Python对日期的一些操作

Python对日期的一些操作_第1张图片

1. 把这种日期 'Mon Jan 29 11:10:49 +0800 2024'
转换成 ‘2024/2/1 10:50:38’

  

这里定义一个func

传入英文日期,返回标准日期格式


def time_formater(input_time_str):
    input_format = '%a %b %d %H:%M:%S %z %Y'
    output_format = '%Y-%m-%d %H:%M:%S'

    return datetime.strptime(input_time_str, input_format).strftime(output_format)

2.获取 上周日期,并if 判断

# get date of 7 days before
weekDayAgo = (datetime.now() - timedelta(days=7)).strftime("%Y-%m-%d %H:%M:%S")
# judge the weibo is new or not
if time_formater('2024/2/1 10:50:38') < weekDayAgo:
    print('日期早了')

Python对日期的一些操作_第2张图片

你可能感兴趣的:(python,日期转换,Python日期,timedelta,strptime)