日時情報の取得方法・datetime †
Pythonで日付や時間を取得するサンプルコードを以下に記します。
スポンサーリンク
関連記事 †
動作確認環境 †
$ python --version
Python 2.7.3
$ lsb_release -d
Description: Ubuntu 12.04.4 LTS
日時の取得サンプルコード †
datetimeモジュールを使用し日付、時間を取得します。
日時の取得サンプルコード †
各要素ごとに取得するサンプルコード †
- サンプルコード
datetime.datetime.today()により取得した各種情報を1つずつ表示します。
import datetime
d = datetime.datetime.today()
print "year: ", d.year
print "month: ", d.month
print "day: ", d.day
print "hour: ", d.hour
print "minute: ", d.minute
print "second: ", d.second
print "microsecond:", d.microsecond
- 実行結果
year: 2014
month: 2
day: 26
hour: 11
minute: 30
second: 38
microsecond: 291912
以上、datetimeモジュールによる日付および時間の取得方法でした。
スポンサーリンク