LoginSignup
0
1

More than 3 years have passed since last update.

Pythonで日時フォーマットを判定してUnixtimeに変換する

Last updated at Posted at 2020-10-13

ソースコードは以下になります。
日時の変換の可否によって判定しています。

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()

実行結果は以下になります。

1602596877

最後まで読んでいただきありがとうございました。
またお会いしましょう。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1