3
2

More than 3 years have passed since last update.

【小ネタ】Pythonでtimestamp取得

Posted at

はじめに

超小ネタで恐縮ですが、表記の手順に関して自分用メモとして

コード

# タイムスタンプ取得

from datetime import datetime
from pytz import timezone
utc_now = datetime.now(timezone('UTC'))
jst_now = utc_now.astimezone(timezone('Asia/Tokyo'))
ts = jst_now.strftime("%Y%m%d-%H%M%S")
print(ts)

結果例

20200106-182252

今回は、ユニークなファイル名の一部を作る目的でこのような書式にしましたが、"%Y%m%d-%H%M%S"の部分を書き換えれば、いろいろなパターンに応用可能です。

3
2
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
3
2