Determine the date and time format in Python and convert to Unixtime

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.

Recommended Posts

Determine the date and time format in Python and convert to Unixtime
Convert timezoned date and time to Unixtime in Python2.7
To represent date, time, time, and seconds in Python
How to get the date and time difference in seconds with python
Get the MIME type in Python and determine the file format
Get the current date and time in Python, considering the time difference
Get date and time in specified format
Convert Python date types to RFC822 format
[Python] How to change the date format (display format)
Get and convert the current time in the system local timezone with python
How to stop a program in python until a specific date and time
[python] Convert date to string
Convert date timezone (time difference) in Python (from string)
Convert the image in .zip to PDF with Python
Convert / return class object to JSON format in Python
I tried to illustrate the time and time in C language
[Python] Display the elapsed time in hours, minutes, and seconds (00:00:00)
Convert markdown to PDF in Python
In the python command python points to python3.8
An easy way to view the time taken in Python and a smarter way to improve it
How to determine the existence of a selenium element in Python
Read big endian binary in Python and convert it to ndarray
[ROS2] How to describe remap and parameter in python format launch
Convert the result of python optparse to dict and utilize it
Released erajp that displays date time in Japanese calendar format in Python
Get the title and delivery date of Yahoo! News in Python
How to display bytes in the same way in Java and Python
Convert psd file to png in Python
MongoDB for the first time in Python
The story of returning to the front line for the first time in 5 years and refactoring Python Django
How to convert DateTimeField format in Django
Convert from Markdown to HTML in Python
How to use is and == in Python
Convert absolute URLs to relative URLs in Python
How to write the correct shebang in Perl, Python and Ruby scripts
Note: [Python3] Convert datetime to a string in any format you like
Convert XML document stored in XML database (BaseX) to CSV format (using Python)
python> array> Determine the number and initialize> mylist = [idx for idx in range (10)] / mylist = [0 for idx in range (10)] >> mylist = [0] * 10
I want to batch convert the result of "string" .split () in Python
Sample code to get the Twitter API oauth_token and oauth_token_secret in Python 2.7
The file name was bad in Python and I was addicted to import
I made a script in python to convert .md files to Scrapbox format
format in python
How to use the C library in Python
3 ways to parse time strings in python [Note]
Convert FBX files to ASCII <-> BINARY in Python
Convert "number" of excel date to python datetime
How to generate permutations in Python and C ++
Convert PDFs to images in bulk with Python
To dynamically replace the next method in python
Convert Windows epoch values to date with python
About the difference between "==" and "is" in python
Draw graphs in Julia ... Leave the graphs to Python
A clever way to time processing in Python
Send messages to Skype and Chatwork in Python
How to convert SVG to PDF and PNG [Python]
The trick to write flatten concisely in python
Experiment and leave evidence to determine the specifications.
How to get the files in the [Python] folder
Convert exponential notation float to str in Python
I want to display the progress in Python!