Python:日付と時間のタイプ学習

5865 ワード

背景
非開発環境では常に日付計算をしなければならないので、Pythonを使う準備をして、ついでに勉強の痕跡をメモします.
コード#コード#
 1  1 # coding = utf-8
 2  2 
 3  3 from datetime import *
 4  4 
 5  5 ##########################    ##########################
 6  6 date_now = date.today()
 7  7 print('now:', date_now)
 8  8 print('year:', date_now.year)
 9  9 print('month:', date_now.month)
10 10 print('day:', date_now.day)
11 11 
12 12 date_tomorrow = date_now.replace(day = date_now.day + 1)
13 13 print('tomorrow:', date_tomorrow)
14 14 
15 15 delta = date_tomorrow - date_now
16 16 print('tomorrow - now:', delta)
17 17 
18 18 ##########################    ##########################
19 19 time_now = time(13, 59, 59)
20 20 print('now time:', time_now)
21 21 print('hour:', time_now.hour)
22 22 print('minute:', time_now.minute)
23 23 print('second:', time_now.second)
24 24 print('next hour time:', time_now.replace(hour = time_now.hour + 1))
25 25 
26 26 ##########################      ##########################
27 27 now = datetime.today()
28 28 print('date:', now.date())
29 29 print('time:', now.time())
30 30 
31 31 ##########################     ##########################
32 32 print(datetime.today().strftime( '%Y-%m-%d %H:%M:%S %f' ) )