0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

python datetime練習

Posted at

PythonでUNIX時間(エポック秒)と日時datetimeを相互変換 | note.nkmk.me
Pythonのdatetimeで日付や時間と文字列を変換(strftime, strptime) | note.nkmk.me

dt_example.py
import datetime

def main():
    # 本日を取得
    dt_today = datetime.datetime.today()
    print(dt_today.strftime('%Y-%m-%d %H:%M:%S'))
    # unixtime
    unixtime = dt_today.timestamp()
    print(unixtime)
    # unixtime -> datetime object
    dt_obj_fromunix = datetime.datetime.fromtimestamp(unixtime)
    print(dt_obj_fromunix.strftime('%Y-%m-%d %H:%M:%S'))
    # datestr -> datetime object
    dt_str = '2020/12/01 12:00:00'
    dt_obj = datetime.datetime.strptime(dt_str, '%Y/%m/%d %H:%M:%S')
    print(dt_obj.strftime('%Y-%m-%d %H:%M:%S'))


if __name__ == '__main__':
    main()
2020-12-12 16:04:38
1607756678.533729
2020-12-12 16:04:38
2020-12-01 12:00:00
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?