How do I check if a string can be parsed to a date?
These are all valid dates. If there's any concern regarding the lack of space in between stuff in item #3 and the last item above, that can be easily remedied via automatically inserting a space in between letters/characters and numbers, if so needed.
But first, the basics:
I tried putting it in an if statement
:
if datetime.strptime(item, '%Y') or datetime.strptime(item, '%b %d %y') or datetime.strptime(item, '%b %d %Y') or datetime.strptime(item, '%B %d %y') or datetime.strptime(item, '%B %d %Y'):
But that's in a try-except block, and keeps returning something like this:
16343 time data 'JUNE1890' does not match format '%Y'
Unless, it met the first condition in the if
statement.
To clarify, I don't actually need the value of the date - I just want to know if it is. Ideally, it would've been something like this:
if item is date: print date else: print "Not a date"
Is there any way to do this?
Using the Date. One way to check if a string is date string with JavaScript is to use the Date. parse method. Date. parse returns a timestamp in milliseconds if the string is a valid date.
split('/') isValidDate = True try: datetime. datetime(int(year), int(month), int(day)) except ValueError: isValidDate = False if(isValidDate): print("Input date is valid ..") else: print("Input date is not valid..") You can also download this program from here.
The parse
function in dateutils.parser
is capable of parsing many date string formats to a datetime
object.
If you simply want to know whether a particular string could represent or contain a valid date, you could try the following simple function:
from dateutil.parser import parse def is_date(string, fuzzy=False): """ Return whether the string can be interpreted as a date. :param string: str, string to check for date :param fuzzy: bool, ignore unknown tokens in string if True """ try: parse(string, fuzzy=fuzzy) return True except ValueError: return False
Then you have:
>>> is_date("1990-12-1") True >>> is_date("2005/3") True >>> is_date("Jan 19, 1990") True >>> is_date("today is 2019-03-27") False >>> is_date("today is 2019-03-27", fuzzy=True) True >>> is_date("Monday at 12:01am") True >>> is_date("xyz_not_a_date") False >>> is_date("yesterday") False
parse
might recognise some strings as dates which you don't want to treat as dates. For example:
Parsing "12"
and "1999"
will return a datetime object representing the current date with the day and year substituted for the number in the string
"23, 4"
and "23 4"
will be parsed as datetime.datetime(2023, 4, 16, 0, 0)
.
"Friday"
will return the date of the nearest Friday in the future."August"
corresponds to the current date with the month changed to August.Also parse
is not locale aware, so does not recognise months or days of the week in languages other than English.
Both of these issues can be addressed to some extent by using a custom parserinfo
class, which defines how month and day names are recognised:
from dateutil.parser import parserinfo class CustomParserInfo(parserinfo): # three months in Spanish for illustration MONTHS = [("Enero", "Enero"), ("Feb", "Febrero"), ("Marzo", "Marzo")]
An instance of this class can then be used with parse
:
>>> parse("Enero 1990") # ValueError: Unknown string format >>> parse("Enero 1990", parserinfo=CustomParserInfo()) datetime.datetime(1990, 1, 27, 0, 0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With