Python 日期处理


## 格式化时间输出

d1 = datetime.datetime(2016,11,12)
d2 = datetime.datetime(2016,1,12)
print d1.strftime("%d/%b/%y")	# 12/Nov/16
print d2.strftime("%Y-%m-%d")	# 2016-01-12


## 计算两个日期之间差多少天

print d2-d1						# -305 days, 0:00:00
datetime.datetime.now()+datetime.timedelta(days=-10)	# 2016-04-29 14:28:28.771000


python datetime与字符串互转

  (2012-08-29 19:44:05)
转载
  datetime转成字符串:
date=datetime.datetime.now()
str=date.strftime("%Y-%m-%d %H:%M:%S")
print(date)
print(str)
输出:
2012-08-29 19:45:57.997000
2012-08-29 19:45:57

字符串转成datetime:
    str = '2012-08-29 19:45:57'
    date = time.strptime(str, "%Y-%m-%d %H:%M:%S")
    print(date)
输出
time.struct_time(tm_year=2012, tm_mon=8, tm_mday=29, tm_hour=19, tm_min=45, tm_sec=57, tm_wday=2, tm_yday=242, tm_isdst=-1)




Ref: python中关于时间和日期函数的常用计算总结(time和datatime)_python_脚本之家 http://www.jb51.net/article/34621.htm

你可能感兴趣的:(Python)