Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting DDMMYYYY with dateutil.parser

I have the following string input : 24052017. When I try to do:

>>>dateutil.parser.parse("24052017")

It tells me that month must be in 1..12.

I have even tried doing:

>>>dateutil.parser.parse("24052017", firstday=True)

It gives me exactly th same outcome.

What seems to happening is that it does not like the fact that there are no spaces or separators. it reads the day correctly, but when it comes to the month it reads 0520. This is what I suspect, at least.

How to I convert this specific input using dateutil.parser, without manipulating the string?

like image 372
Renier Avatar asked Jun 02 '17 13:06

Renier


4 Answers

This format is currently not supported by dateutil. In general, if you know the format of your date and it does not have time zones, you should just use datetime.datetime.strptime to parse your dates, as dateutil.parser.parse has a considerable amount of overhead that it uses trying to figure out what format your date is in, and, critically, it may get that format wrong.

There is a pull request against the 2.6.0 branch that is under debate to add this format, you can find it here, ondateutil's github. The main argument against this would be that if you are trying to parse a series of dates, it will interpret 12052017 as "December 5, 2017", but 13052017 as "May 13, 2017". (That said, you do have the same inconsistency now in that the first date will parse to December 5, 2017, but the second date will simply fail).

If you do not know the format of the string, but you know that if it is an 8-digit numerical date you want it to be interpreted as DDMMYYYY, for now your best bet is to hard-code that exception into your parser:

from dateutil.parser import parse as duparse
from datetime import datetime

def parse(dtstr, *args, **kwargs):
    if len(dtstr) == 8 and dtstr.isnumeric():
        return datetime.strptime(dtstr, '%d%m%Y')
    else:
        return duparse(dtstr, *args, **kwargs)

There is some slow-moving planned effort to provide a more flexible and extensible parser for dateutil, but not much work has been done on this yet.

like image 133
Paul Avatar answered Nov 04 '22 03:11

Paul


If you're not precious about using dateutil, you could do this with datetime.datetime.strptime:

from datetime import datetime

print datetime.strptime("24052017", '%d%m%Y')

This returns (in yyyy-mm-dd hh:mm:ss)

2017-05-24 00:00:00
like image 45
asongtoruin Avatar answered Nov 04 '22 05:11

asongtoruin


Well, dateutil.parser.parse needs some hints about date format you're trying to parse; in lack of such hints it assumes YYYYMMDD format, so your input becomes equivalent to 2405-20-17; either rearrange your string to read 20170524, for example like this dateutil.parser.parse(d[4:8]+d[2:4]+d[0:2]), or use separators: dateutil.parser.parse("24.05.2017") will work (however, the former method is preferred, due to ambiguity of the latter).

like image 2
Błotosmętek Avatar answered Nov 04 '22 03:11

Błotosmętek


You should be using datetime library as mentioned in the asongtoruin' answer. But if you want to achieve this using the dateutil.parser, you have to firstly convert your string to the format understandable to dateutil. Below is the example:

>>> d_string = "24052017"

#                                                    to consider day before month v
>>> dateutil.parser.parse('/'.join([d_string[:2], d_string[2:4],d_string[4:]]), dayfirst=True)
datetime.datetime(2017, 5, 24, 0, 0)

Here I am converting "24052017" to "24/05/2017" before it is passed to the dateutil.parser.parse(...).

like image 1
Moinuddin Quadri Avatar answered Nov 04 '22 04:11

Moinuddin Quadri