【Python】 时间计算

说实在的不喜欢Python,这主要是相对于PHP而言的,Python在Web相关领域里面,缺少了PHP的丰厚家学和积淀,但是有时候没有办法,还是得学一点的。在PHP里,用惯了strtotime这种神奇函数,在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

from datetime import date
from datetime import datetime
from datetime import timedelta

today = date.today()
print today
#2012-12-08

now = datetime.today()
print now
#2012-12-08 14:13:28.298156

last_monday = today - timedelta(days=today.weekday())
print last_monday
#2012-12-03

next_monday = today + timedelta(days=-today.weekday(), weeks=1)
print next_monday
#2012-12-10

last_month_end = date(today.year, today.month, 1) - timedelta(days=1)
last_month_start = date(last_month_end.year, last_month_end.month, 1)
print last_month_end
#2012-11-30
print last_month_start
#2012-11-01