Python 日期处理
#!/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#######################