2
我有一個函數,得到提供的日期時間前一個月開始:搬運月
def get_start_of_previous_month(dt):
'''
Return the datetime corresponding to the start of the month
before the provided datetime.
'''
target_month = (dt.month - 1)
if target_month == 0:
target_month = 12
year_delta = (dt.month - 2)/12
target_year = dt.year + year_delta
midnight = datetime.time.min
target_date = datetime.date(target_year, target_month, 1)
start_of_target_month = datetime.datetime.combine(target_date, midnight)
return start_of_target_month
然而,似乎很令人費解。任何人都可以提出更簡單的方法嗎我正在使用Python 2.4。