0

I'm recieving date in PMS message something like this |GA090616|GD090617| which means Guest Arrival is at 09-06-16 and Guest Deprature is at 09-06-17

I wanted to parse it as date using python.

I've also visited stack oveflow[1, 2 ...] for this but as solution I've found

from datetime import datetime

self.DATE_FROMAT='%d/%m/%y'

arrival_date=datetime.strptime('90616', self.DATE_FROMAT).date()
print(arrival_date)

and it's not possible to parse it like this due to its unclear format. I'm not sure if 09 is a month or a date, but from what I've seen in documents and PDFs, it appears to be a month.

Is there any better solution for this kind of date parsing? or suggestions for my expectations.

09-06-16,
09-06-17

Note: Please Just take the date from the string 090617 and parse it as a date. That will be helpful.

M.Abdullah Iqbal
  • 219
  • 1
  • 17

1 Answers1

1

You can do this with regex matching, you can either split the string with msg.split("|") or not, but that depends on your use case.

import re
from datetime import datetime

msg = "GA090616|GD090617|"
DATE_FORMAT='%d%m%y'

ar = re.match("GA(\d{6})", msg)
dp = re.match("GD(\d{6})", msg)

guest_arrival = datetime.strptime(ar.group(1), DATE_FORMAT).date()
guest_departure = datetime.strptime(dp.group(1), DATE_FORMAT).date()

Although not fully tested, this should be a boilerplate as to how to retrieve the date from the message. Remember to remove the \ from the date format, as that is not included in the message.

user20223018
  • 486
  • 1
  • 5
  • ``` Traceback (most recent call last): File "/home/abdullah/Abdullah Iqbal/Projects/python/pythonCommon/DateParsing.py", line 11, in guest_departure = datetime.strptime(dp.group(1), DATE_FORMAT).date() AttributeError: 'NoneType' object has no attribute 'group' ``` Found this Error. Same as I've tried, don't need to parse anything as I've already made a scenario. Just take the date from string `090617` and Need it to be parsed as date. that'll be help full, and thanks for taking time for me. – M.Abdullah Iqbal Feb 01 '23 at 16:47
  • What's wrong with this? DATE_FORMAT='%d%m%y' and guest_arrival = datetime.strptime("090617", DATE_FORMAT).date() – user20223018 Feb 02 '23 at 10:54
  • The above Error happened, it's not working on that particular string even though I'm not able to split it. I just wanted to be on in this format 'dd-MM-YY' – M.Abdullah Iqbal Feb 02 '23 at 12:10