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 5 years have passed since last update.

よく使うpythonの日付変換まとめ

Last updated at Posted at 2019-05-21

これだけ知りたかったんで。。

time.py
# unixタイム
s = time.time()
# →1558429052.4563782

# unixタイムの整数のみ
ss = int(s)
# →1558429052

# unixタイム→datetime型
sss = datetime.datetime.fromtimestamp(ss)
# → datetime.datetime(2019, 5, 21, 17, 57, 32)

# unixタイム→日付文字(YYYY-MM-DD H:i:s)
date_str = "{0:%Y-%m-%d %H:%M:%S}".format(sss)
# → '2019-05-21 17:57:32'

# 日付文字→datetime型
date_datetime = datetime.datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S')
# → datetime.datetime(2019, 5, 21, 17, 57, 32)

# datetime型→unixタイム
date_datetime.timestamp()
# → 1558429052.0

追記(unixタイム <-> 日付文字)

unix.py

# unixタイム→日付文字(YYYY-MM-DD H:i:s)
unix = time.time()
"{0:%Y-%m-%d %H:%M:%S}".format(datetime.datetime.fromtimestamp(unix))
# '2019-05-22 16:59:38'

# 日付文字(YYYY-MM-DD H:i:s)→unixタイム
datetime.datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S').timestamp()
# 1558511978.0
0
0
1

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?