The source code is as follows. Judgment is based on whether the date and time can be converted.
import datetime
def validate_datetime(date_str):
try:
date_obj = datetime.datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S')
except:
print('Invalid Datetime Format')
return False
return date_obj
def main():
date_str = '2020-10-13 22:47:57'
result = validate_datetime(date_str)
if result:
print(int(result.timestamp()))
if __name__ == '__main__':
main()
The execution result is as follows.
1602596877
Thank you for reading until the end. Let's meet again.