[英]Converting a formatted string into date time
I have a string of this format:我有一个这种格式的字符串:
a~b~c~[character][year]
eg:
x = a~b~c~f16 represent january 2016.
Below os the character and month mapping:下面是字符和月份的映射:
month_dict = {'F': 1,
'G': 2,
'H': 3,
'J': 4,
'K': 5,
'M': 6,
'N': 7,
'Q': 8,
'U': 9,
'V': 10,
'X': 11,
'Z': 12
}
I want to find out the last business date of this month in yyyymmdd format.我想以 yyyymmdd 格式找出本月的最后一个营业日期。 Can someone help me in this?
有人可以帮助我吗?
I am trying to do the following:我正在尝试执行以下操作:
m = x.split(~)
def last_business_day_in_month(year: int, month: int) -> int:
return max(calendar.monthcalendar(year, month)[-1:][0][:5])
x = last_business_day_in_month(m[3][1:], month_dict[3][0])
Use regex
, datetime
and calendar
使用
regex
、 datetime
和calendar
import re
import datetime
import calendar
month_dict = {'F': 1,
'G': 2,
'H': 3,
'J': 4,
'K': 5,
'M': 6,
'N': 7,
'Q': 8,
'U': 9,
'V': 10,
'X': 11,
'Z': 12
}
x = f"a~b~c~f16"
result = re.search("[f-z]\d+", x).group(0)
letter = result[0]
year = int(result[1:])
month = month_dict[letter.upper()]
last_business_day = max(calendar.monthcalendar(year, month)[-1:][0][:5])
date = datetime.datetime.strptime(f"{last_business_day} {month} {year}", "%d %m %y")
formatted_date = date.strftime("%Y%m%d")
print(formatted_date)
Output Output
20160129
def last_business_day_in_month(year: int, month: int) -> int:
return max(calendar.monthcalendar(year, month)[-1:][0][:5])
m = x.split("~")
year = int(m[3][1:])
month = month_dict[m[3][0].upper()]
x = last_business_day_in_month(year, month)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.