I am working with a feature to generate a duration once a date(can be incomplete) is specified by the user.
user inputs can be in any form.
I am using this super cool dateutil.parse which is working smoothly in capturing date information. It can also capture incomplete dates and set what missing to a default value. Ex:
out_date = dparser.parse("2021",default=datetime.datetime(2021, 1, 1))
#out 2021-01-01 00:00:00
out_date = dparser.parse("2021 june",default=datetime.datetime(2021, 1, 1))
#out 2021-06-01 00:00:00
out_date = dparser.parse("2021 sep 17",default=datetime.datetime(2021, 1, 1))
#out 2021-09-17 00:00:00
As my concern is to return a period, I want to differentiate between inputs "2021" and "2021-01-01" where the output will be the same with the above method.
Basically, I want is to capture the missing values so that I can process those as I wish. Ex:
input = "2021" > output = (2021, false, false)
input = "2021 jan" > output = (2021, 01, false)
input = "2021 jan 15" > output = (2021, 01, 15)
Any help would be highly appreciated.
PS: inputs can be out of structure such as "2021/01/01", "January 15, 2021", "January 2021"
The following trick worked for me. Hope this would help anybody looking for such a scenario.
import datetime
import dateutil.parser as dparser
def date_checker(date_input):
out_dict = ['false','false','false']
out_check_a = dparser.parse(date_input,default=datetime.datetime(2020, 1, 1))
out_check_b = dparser.parse(date_input, default=datetime.datetime(2021, 12, 31))
if(out_check_a.year==out_check_b.year):
out_dict[0] = out_check_a.year
if (out_check_a.month == out_check_b.month):
out_dict[1] = out_check_a.month
if (out_check_a.day == out_check_b.day):
out_dict[2] = out_check_a.day
return out_dict
print(date_checker('2021')) #output [2021, 'false', 'false']
print(date_checker('2021 jan')) #output [2021, 1, 'false']
print(date_checker('2021 jan 15')) #output [2021, 1, 15]
print(date_checker('2021/01/01')) #output [2021, 1, 1]
print(date_checker('January 15, 2021')) #output [2021, 1, 15]
print(date_checker('January 2021')) #output [2021, 1, 'false']