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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

``` Traceback (most recent call last): File "/home/abdullah/Abdullah Iqbal/Projects/python/pythonCommon/DateParsing.py", line 11, in <module> 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.
What's wrong with this? DATE_FORMAT='%d%m%y' and guest_arrival = datetime.strptime("090617", DATE_FORMAT).date()
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'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.