如何从 python 中的 YYYYMM 字符串日期中减去月份?
How to subtract months from YYYYMM string date in python?
我有日期字符串(YYYYMM
例如:201909
.
在Python中,如何简单地减去一个月?
from datetime import datetime
Month = '201905'
Month = datetime.strptime(Month, '%Y%m').date()
Month1 = str(Month -1)
Month2 = str(Month -2)
Month3 = str(Month -3)
Month6 = str(Month -6)
Month12 = str(Month - 100)
这不起作用,但我希望能够将 'Month' 变量设置为类似“201905”的字符串,并从那里计算各种 YYYYMM 字符串。
我正在利用它是一个字符串这一事实。
使用
从字符串中提取最后两个 MM
Month[-2:] -> this will give you 01 - 12 range.
您需要检查的唯一条件是它何时为 0。(我假设您将始终获得有效的 YYYYMM。
Month = '201912'
month = 12 if (int(Month[-2:]) -1 ) % 12 == 0 else (int(Month[-2:]) -1 ) % 12
year = int(Month[:-2]) - 1 if month == 12 else int(Month[:-2])
print (str(year)+str(month))
这将给出所需的月份 int
:
Month = '201905'
Month = datetime.strptime(Month, '%Y%m').date()
month = Month.month # this is an int, you can easily subtract it
print (Month.month)
以下适用于最多 12 个月的任意减量
old_date_string = '201901'
decrement = 1
year = int(old_date_string[:-2])
month = int(old_date_string[-2:])
month = month - decrement
if month<1:
month = month + 12
year = year - 1
new_date_string = str(year) + '{:02d}'.format(month)
print(new_date_string)
此解决方案既简单又稳健。它利用经过时间考验的内置包,将带您回到前一年,而不仅仅是从月份数字中减去 (n)。
from datetime import datetime as dt
from dateutil.relativedelta import relativedelta
# User's original values.
string = '201905'
dte = dt.strptime(string, '%Y%m').date()
# Calculate one months previous.
result = dte + relativedelta(months=-1)
# Display path and results
print('Original string value: {}'.format(string))
print('Original datetime value: {}'.format(dte))
print('Result (original minus two months): {}'.format(result))
输出:
Original string value: 201905
Original datetime value: 2019-05-01
Result (original minus one month): 2019-04-01
我有日期字符串(YYYYMM
例如:201909
.
在Python中,如何简单地减去一个月?
from datetime import datetime
Month = '201905'
Month = datetime.strptime(Month, '%Y%m').date()
Month1 = str(Month -1)
Month2 = str(Month -2)
Month3 = str(Month -3)
Month6 = str(Month -6)
Month12 = str(Month - 100)
这不起作用,但我希望能够将 'Month' 变量设置为类似“201905”的字符串,并从那里计算各种 YYYYMM 字符串。
我正在利用它是一个字符串这一事实。 使用
从字符串中提取最后两个 MMMonth[-2:] -> this will give you 01 - 12 range.
您需要检查的唯一条件是它何时为 0。(我假设您将始终获得有效的 YYYYMM。
Month = '201912'
month = 12 if (int(Month[-2:]) -1 ) % 12 == 0 else (int(Month[-2:]) -1 ) % 12
year = int(Month[:-2]) - 1 if month == 12 else int(Month[:-2])
print (str(year)+str(month))
这将给出所需的月份 int
:
Month = '201905'
Month = datetime.strptime(Month, '%Y%m').date()
month = Month.month # this is an int, you can easily subtract it
print (Month.month)
以下适用于最多 12 个月的任意减量
old_date_string = '201901'
decrement = 1
year = int(old_date_string[:-2])
month = int(old_date_string[-2:])
month = month - decrement
if month<1:
month = month + 12
year = year - 1
new_date_string = str(year) + '{:02d}'.format(month)
print(new_date_string)
此解决方案既简单又稳健。它利用经过时间考验的内置包,将带您回到前一年,而不仅仅是从月份数字中减去 (n)。
from datetime import datetime as dt
from dateutil.relativedelta import relativedelta
# User's original values.
string = '201905'
dte = dt.strptime(string, '%Y%m').date()
# Calculate one months previous.
result = dte + relativedelta(months=-1)
# Display path and results
print('Original string value: {}'.format(string))
print('Original datetime value: {}'.format(dte))
print('Result (original minus two months): {}'.format(result))
输出:
Original string value: 201905
Original datetime value: 2019-05-01
Result (original minus one month): 2019-04-01