6

I'm trying to see if a list of dates are valid dates. I'm using the dateutil library, but I'm getting weird results. For example, when I try the following:

import dateutil.parser as parser
x = '10/84'
date = (parser.parse(x))
print(date.isoformat())

I get the result 1984-10-12T00:00:00 which is wrong. Does anyone know why this 12 gets added to the date?

Nayuki
  • 17,911
  • 6
  • 53
  • 80
ray smith
  • 380
  • 3
  • 14

2 Answers2

7

The parse() method parses the string and updates a default datetime object, using the parsed information. If the default is not passed into this function, it uses first second of today.

This means that the 12 in your result, is today (when you're running the code), only the year and the month are updated from parsing the string.

If you need to parse the date string but you're not sure if it's a valid date value, then you may use a try ... except block to catch parse errors.

import dateutil.parser as parser
x = '10/84'
try:
    date = (parser.parse(x))
    print(date.isoformat())
except ValueError as err:
    pass # handle the error
farzad
  • 8,775
  • 6
  • 32
  • 41
  • How do I avoid this to have an error thrown stating that this is not a date? – ray smith Oct 13 '15 at 03:00
  • Are you getting an error when running this code sample? If so, could you please provide more information about the error? – farzad Oct 13 '15 at 03:02
  • No, I'm not getting an error. It's behaving like you stated. if x = '10/84' I want to throw an error. – ray smith Oct 13 '15 at 03:02
  • If you want to make sure that you'll have control on what happens when you parse a string that is not a valid date (for example the value is provided by user input or etc.), then (as documented for the parse() function) it throws a `ValueError`. So you could put the code in a `try ... except` block to catch the error. – farzad Oct 13 '15 at 03:04
  • If the user input x = '10/84' there is no error thrown, because to the parser this is a valid date( based on the fact that I don't pass a default datetime object). I do not want this. I want an error to be throw if the user input a date in the format 'month/year' – ray smith Oct 13 '15 at 03:06
1

12 is the current date . dateutil takes components from current date/time to account for missing date or year in the date (it does not do this for the month, only date or year). Like another example would be a date like - Janauary 20 - this would get parsed as 2015/01/12 taking the 2015 year from the current datetime.

Sadly I have not yet found any options or such to stop this behavior.

I believe the best option for you would be to come up with a list of the valid datetime formats that you are expecting , and then manually try datetime.datetime.strptime on them , excepting ValueError . Example -

def isdate(dt, fmt):
    try:
        datetime.datetime.strptime(dt, fmt)
        return True
    except ValueError:
        return False

validformats = [...]
dates =[...]

for x in dates:
    if any(isdate(x,fmt) for fmt in validformats):
        print(x, 'is valid date')
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176