Search code examples
python-3.xdateparser

date parser solution python


while parsing the date using 'dateutil parser' and even by 'moment parser' in python for the date dd/mm/yyyy eg 04/05/2019 it reflects this 05/04/2019 i.e it takes month as the date and vice versa. while if i try the same thing for a date greater than 12 will give the correct output, any solutions ?

import moment
print(moment.date('05/04/2019')) //date:05 month:04 year:2019

output: 2019-05-04T00:00:00+05.50 //considers date as month and vice versa which is wrong.

but if i try this

import moment
print(moment.date('13/04/2019')) //date:12 month:04 year:2019

output: 2019-04-13T00:00:00+05.50


Solution

  • Use dayfirst=True

    Ex:

    from dateutil.parser import parse 
    
    print(parse('05/04/2019',dayfirst=True))
    # --> 2019-04-05 00:00:00