【发布时间】:2021-05-19 18:55:05
【问题描述】:
我有一个str 变量,即my_variable = str(201705)。
我想在 python 中使用 datetime 获取 my_variable 的上个月。有没有办法做到这一点?到目前为止,我有:
from datetime import datetime
my_variable = str(201705)
month = datetime.strptime(my_variable, '%Y%m').strftime("%b%Y")
这为我提供了 str 格式的当前变量月份,所以是 May2017。我只想将上个月存储在一个新变量中。所以my_new_month 将 = 四月。
这是否可以使用 python 日期时间库,如果没有,是否有其他解决方案来完成此操作?
【问题讨论】: