LoginSignup
0
0

More than 5 years have passed since last update.

Python3で現在時のスタンプを作成する方法

Last updated at Posted at 2019-01-03

ファイル名に実行時刻を付与したいときによく使うのでメモとして書いておきます。

参考にしたのはsamuraさんの記事。-> https://www.sejuku.net/blog/23606

以下では日時のスタンプを付与してます。

timestamp.py
# Import
import datetime

# Create Time Stamp using zero fill
date = datetime.datetime.now()
time_stamp = '%04d%02d%02d' % (date.year, date.month ,date.day)
print(time_stamp)

利用例

create_file.py
f = open('sample%s.txt' % time_stamp ,'w')
f.write( str(time_stamp) + '\n')
f.close()

年から秒まで以下の方法で取得可能
https://www.sejuku.net/blog/23606

samples
print("年:",date.year) 
print("月:",date.month)
print("日:",date.day) 
print("時:",date.hour)
print("分:",date.minute)
print("秒:",date.second)
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