[Solved] validate date format in python [with regex or any built in method]


you can use datetime for this (in python 2.7)

from datetime import datetime

date_string = '20170808'

if len(date_string) == 8:
   datetime.strptime(date_string, '%Y%m%d')
else:
   raise ValueError('date need to be in format YYYYMMDD ')

if you date is not valid than you get:

a ValueError Exception

ValueError: unconverted data remains: 0

2

solved validate date format in python [with regex or any built in method]