ページへ戻る

− Links

 印刷 

Python​/現在の日付と時刻の取得 の変更点 :: NJF Wiki

xpwiki:Python/現在の日付と時刻の取得 の変更点

« Prev[3]  
1: 2018-01-05 (金) 04:25:34 njf[4] ソース[5] バックアップ No.1 を復元して編集[6] 現: 2018-01-05 (金) 12:30:38 njf[4] ソース[7] 編集[8]
Line 1: Line 1:
-Pythonでの日付の処理はdatetimeを使います。現在時刻の取得にはnowメソッドを使います。+Pythonでの日付の処理は「datetime」を使います。現在時刻の取得には「now」メソッドを使います。
 from datetime import datetime  from datetime import datetime
 now = datetime.now()  now = datetime.now()
 +
 +書式付きで出力するには「strftime」を使います。
 +
 + from datetime import datetime
 + 
 + now = datetime.now()
 + print now.strftime("%Y/%m/%d %H:%M:%S")
 +
 +結果
 + 2018/01/05 13:21:37
 +
 +個別に各値を取得するには以下のように各プロパティにアクセスします。
 +
 + print now.year
 + 
 + print now.month
 + 
 + print now.day
 + 
 + print now.hour
 + 
 + print now.minute
 + 
 + print now.second
 +
 +結果
 +
 + 2018
 + 1
 + 5
 + 13
 + 21
 + 37
« Prev[3]