• datetime 获取本周一日期:
import datetime as dt
monday_time = dt.datetime.strftime((dt.date.today()-dt.timedelta(dt.date.today().weekday())), '%Y-%m-%d')
  • datetime 获取上月初日期:
import datetime as dt
last_month=(dt.date.today().replace(day=1)+ dt.timedelta(days=-1)).replace(day=1)
  • datetime 获取本月初日期:
import datetime as dt
now_month=dt.date.today().replace(day=1)
  • 将“2021/11/11 8:45”的日期格式变为“2021-11-11”

strftime与strptime:
strptime(),按照特定时间格式将字符串转换为时间类型。
strftime(),将时间类型转为特定时间格式字符串。(str from time )

import datetime as dt
s = '2021/11/11 8:45'
a = dt.datetime.strptime(s,'%Y/%m/%d %H:%M')
b = dt.datetime.strftime(a,'%Y-%m-%d')
print('b=',b)