Help us understand the problem. What is going on with this article?

python 現在時刻取得

More than 3 years have passed since last update.
now_time.py
from datetime import datetime
datetime.now().strftime("%Y/%m/%d %H:%M:%S")

現在日時のdatetimeオブジェクトを取得

from datetime import datetime

コマンド 結果
datetime.now() datetime.datetime(2014, 1, 2, 3, 4, 5, 000000)

datetimeオブジェクトを加算する

from datetime import datetime, timedelta

コマンド 結果
datetime(2014,1,2,3,4,5) datetime.datetime(2014, 1, 2, 3, 4, 5)
datetime(2014,1,2,3,4,5) + timedelta(weeks=1) datetime.datetime(2014, 1, 9, 3, 4, 5)
datetime(2014,1,2,3,4,5) + timedelta(days=1) datetime.datetime(2014, 1, 3, 3, 4, 5)
datetime(2014,1,2,3,4,5) + timedelta(hours=1) datetime.datetime(2014, 1, 2, 4, 4, 5)
datetime(2014,1,2,3,4,5) + timedelta(minutes=1) datetime.datetime(2014, 1, 2, 3, 5, 5)
datetime(2014,1,2,3,4,5) + timedelta(seconds=1) datetime.datetime(2014, 1, 2, 3, 4, 6)
datetime(2014,1,2,3,4,5) + timedelta(milliseconds=1) datetime.datetime(2014, 1, 2, 3, 4, 5, 1000)
datetime(2014,1,2,3,4,5) + timedelta(microseconds=1) datetime.datetime(2014, 1, 2, 3, 4, 5, 1)

datetimeオブジェクトを減算する

from datetime import datetime, timedelta

コマンド 結果
datetime(2014,1,2,3,4,5) datetime.datetime(2014, 1, 2, 3, 4, 5)
datetime(2014,1,2,3,4,5) + timedelta(weeks=-1) datetime.datetime(2013, 12, 26, 3, 4, 5)
datetime(2014,1,2,3,4,5) + timedelta(days=-1) datetime.datetime(2014, 1, 1, 3, 4, 5)
datetime(2014,1,2,3,4,5) + timedelta(hours=-1) datetime.datetime(2014, 1, 2, 2, 4, 5)
datetime(2014,1,2,3,4,5) + timedelta(minutes=-1) datetime.datetime(2014, 1, 2, 3, 3, 5)
datetime(2014,1,2,3,4,5) + timedelta(seconds=-1) datetime.datetime(2014, 1, 2, 3, 4, 4)
datetime(2014,1,2,3,4,5) + timedelta(milliseconds=-1) datetime.datetime(2014, 1, 2, 3, 4, 4, 999000)
datetime(2014,1,2,3,4,5) + timedelta(microseconds=-1) datetime.datetime(2014, 1, 2, 3, 4, 4, 999999)

datetimeオブジェクト → 数値

from datetime import datetime

コマンド 結果
datetime(2014,1,2,3,4,5).year 2014
datetime(2014,1,2,3,4,5).month 1
datetime(2014,1,2,3,4,5).day 2
datetime(2014,1,2,3,4,5).hour 3
datetime(2014,1,2,3,4,5).minute 4
datetime(2014,1,2,3,4,5).second 5

datetimeオブジェクト → 文字列

from datetime import datetime

コマンド 結果 補足
datetime(2014,1,2,3,4,5).strftime('%Y/%m/%d %H:%M:%S') 2014/01/02 03:04:05
datetime(2014,1,2,3,4,5).strftime('%Y') 2014
datetime(2014,1,2,3,4,5).strftime('%y') 14 年(下2桁)
datetime(2014,1,2,3,4,5).strftime('%m') 01
datetime(2014,1,2,3,4,5).strftime('%d') 02
datetime(2014,1,2,3,4,5).strftime('%H') 03 時 (24時間表記)
datetime(2014,1,2,3,4,5).strftime('%I') 03 時 (12時間表記)
datetime(2014,1,2,3,4,5).strftime('%M') 04
datetime(2014,1,2,3,4,5).strftime('%S') 05
datetime(2014,1,2,3,4,5).strftime('%b') Jan 短縮月名
datetime(2014,1,2,3,4,5).strftime('%B') January 月名
datetime(2014,1,2,3,4,5).strftime('%a') Thu 短縮曜日名
datetime(2014,1,2,3,4,5).strftime('%A') Thursday 曜日名
datetime(2014,1,2,3,4,5).strftime('%p') AM AM or PM
datetime(2014,1,2,3,4,5).isoformat() 2014-01-02T03:04:05 ISO 8601 形式
datetime(2014,1,2,3,4,5).ctime() Thu Jan 2 03:04:05 2014 %a %b %d %X %Y
datetime(2014,1,2,3,4,5).strftime('%c') Thu Jan 2 03:04:05 2014 %a %b %d %X %Y
datetime(2014,1,2,3,4,5).strftime('%x') 01/02/14 %m/%d/%y
datetime(2014,1,2,3,4,5).strftime('%X') 03:04:05 %H:%M:%S
datetime(2014,1,2,3,4,5).strftime('%s') 1388599445 unixtime

文字列 → datetimeオブジェクト

from datetime import datetime

コマンド 結果
datetime.strptime('2014-01-02 03:04:05', '%Y-%m-%d %H:%M:%S') datetime.datetime(2014, 1, 2, 4, 3, 5)

datetimeオブジェクト → unixtime

from datetime import datetime

コマンド 結果
datetime(2014,1,2,3,4,5).strftime('%s') 1388599445

unixtime → datetimeオブジェクト

from datetime import datetime

コマンド 結果
datetime.fromtimestamp(1388599445) datetime.datetime(2014, 1, 2, 4, 3, 5)

datetimeオブジェクト → dateオブジェクト

from datetime import datetime

コマンド 結果
datetime(2014,1,2,3,4,5).date() datetime.date(2014, 1, 2)

datetimeオブジェクト → timeオブジェクト

from datetime import datetime

コマンド 結果
datetime(2014,1,2,3,4,5).time() datetime.time(3, 4, 5)
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした