I am using dateutil.parser. I only want to parse a date string which has date, time and timezone info.
e.g. I only want to accept valid date as "2014-11-11T18:28:50.588Z". If user have passed "2013-12-11"(which is again valid date for dateutil), then I want to throw error.
P.S - I know I can use regex but I was hoping to see if I can use dateutil library
No, this is not possible with
dateutil.parser
.dateutil
's parser is primarily used for processing anything that looks like a date and currently has minimal customization options.One thing to note is that both
2013-12-11
and2014-11-11T18:28:50.588Z
are valid ISO-8601 dates, so even if you have a rule saying, "parse only ISO-8601 dates", it would still catch both of these.My recommendation in general is that if you know the exact format of the date string, you should use
strptime
, example:If you want to be a little more flexible and allow dates without the
Z
extension, or without the floats, I have found the fastest way is to do anif
/elif
block that checks the length of the string. Here is how I would do that: