10 examples of 'python check if string is date' in Python

Every line of 'python check if string is date' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
270def is_date(obj):
271 """Returns ``True`` if `obj` is an instance of ``datetime.date``."""
272 return isinstance(obj, datetime.date)

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

60def isdatetime(datestring):
61 try:
62 d = datetime.datetime.strptime(datestring, '%Y-%m-%dT%H:%M:%S')
63 if d:
64 return True
65 else:
66 return False
67 except Exception as e:
68 return False
53def validDate(date_string):
54 try:
55 datetime.datetime.strptime(date_string, _date_format)
56 except ValueError:
57 print("Date should be DD/MM/YYYY")
58 return False
59 return True
16def match_date(s):
17 matches = re.findall('[\\d-]{8,10}', s)
18 if matches:
19 return matches[0]
20 else:
21 return False
46def check_date(s):
47 dateFmt = "[\d]{4}-[\d]{2}-[\d]{2}T[\d]{2}:[\d]{2}:[\d]{2}Z"
48 if not re.match(dateFmt, s):
49 raise ValueError("Incorrect date format: ", s)
51 return True
15def validate(date_text):
16 try:
17 datetime.datetime.strptime(date_text, '%Y-%m-%d')
18 except ValueError:
19 #raise ValueError("Invalid Partition Table File")
20 sys.exit("Invalid Partition Table File")
9def valid_date(s):
10 return datetime.datetime.strptime(s, "%Y-%m-%d")
26def valid_date(s):
27 try:
28 return datetime.strptime(s, "%Y-%m-%d").date()
29 except ValueError:
30 msg = "Not a valid date: '{0}'.".format(s)
31 raise argparse.ArgumentTypeError(msg)
79def test_check_date():
80 assert check.check_date(date(2525, 2, 20))
28def valid_date(s):
29 try:
30 return parse_date(s)
31 except ValueError:
32 msg = "Not a valid date: '{0}'.".format(s)
33 raise argparse.ArgumentTypeError(msg)

Related snippets