1

I have a large sales database the first column of which is the purchase date. The problem is some of these dates are entered in DD.MM.YY format, some in YY.MM.DD and some in YYYY/MM/DD. I want to make them all to same format. What is the cleanest way I can do this?

Note 1: I'm thinking of doing a series of ifs but that would be a lot of conditions so I'm wondering if there is a cleaner shortcut.

Note 2: An additional complication is that the dates are in Jalaali calender and not Gregorian. I have the function that will convert them to gregorian but I need to pass the correct year, month, day arguments to it; this is why I want to bring them all to a single format. But additionally, this means that if you offer some "Gregorian-only" solutions, like dateutil.parser, it might not work.

A.M.
  • 11
  • 3

1 Answers1

0

Immediately after posting this I found/thought of a solution myself, but instead of deleting the question I decided to post the answer in case someone else come to a similar problem.

tl;dr - I just added a century option to dateutil.parser. I didnt know how to but I found this.

Here's my end code:

from khayyam import JalaliDate
from dateutil.parser import parse, parserinfo

class MyParserInfo(parserinfo):
    def convertyear(self, year, *args, **kwargs):
        if year < 100:
            year += 1300
        return year

if __name__ == '__main__':
    dt = parse("9.12.96", MyParserInfo()).date()
    a=JalaliDate(dt.year, dt.month, dt.day).todate()
    print(dt)
    print(a)

#1396-09-12
#2017-12-03
A.M.
  • 11
  • 3