Python日付処理

2688 ワード

Python日付処理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 #!/bin/python #coding=utf-8
  from   datetime import   datetime from   datetime import   timedelta
  ################### Begin###################### #date>>string now =   datetime.now() print   now.strftime( '%Y-%m-%d %H:%M:%S' )
  #string>>date str_date = '2013-11-02 16:26:23' d = datetime.strptime(str_date, '%Y-%m-%d %H:%M:%S' ) print   d ################### End########################
 
  #################### start####################### # d1 =   datetime.strptime( '2013-11-02 16:26:23' , '%Y-%m-%d %H:%M:%S' ) d2 =   datetime.strptime( '2013-10-02 16:26:23' , '%Y-%m-%d %H:%M:%S' ) diff =    d1 - d2 print   diff.days
  # N /N /N now =   datetime.now() aDate =   timedelta(days = 5 ) # n_days =   now + aDate print   n_days.strftime( '%Y-%m-%d %H:%M:%S' ) delta1 =   timedelta(seconds =   600 ) delta2 =   timedelta(weeks =   3 ) print   delta1 + now print   delta2 + now #################### start#######################