python获取本年本周本月本天

python获取本周本月本天

import datetime
from datetime import timedelta

current_time = datetime.datetime.now()

year = current_time.year
month = current_time.month
day = current_time.day

# 自然年
this_year_start = datetime.datetime(year=year, month=1, day=1)
print(this_year_start)

# 自然月
this_month_start = datetime.datetime(year=year, month=month, day=1)
print(this_month_start)

# 自然天
this_day_start = datetime.datetime(year=year, month=month, day=day)
print(this_day_start)

# 自然周
this_week_start = this_day_start - timedelta(days=current_time.weekday(), )
print(this_week_start)


输出:
2021-01-01 00:00:00
2021-04-01 00:00:00
2021-04-20 00:00:00
2021-04-19 00:00:00

你可能感兴趣的:(python,python学习,python)