标签:
# import datetime
# String to datetime:
# Notice:
# String 转换datetime类型时,日期的格式要匹配
a = ‘2016-03-23‘ b = datetime.datetime.strptime(a,‘%Y-%m-%d‘)
# datetime to String:
# Notice:
# datetime 转换为String 类型时,可以转换为任意格式
now = datetime.datetime.now()
# 获取当前时间:
now = datetime.datetime.now()
# 获取今天日期:
today = datetime.date.today()
# 获取昨天日期:
yesterday = today - datetime.timedelta(days=1)
# 获取某个日期的前后n天,n小时:
# 例如:获取现在之前8天的日期
now = datetime.datetime.now()
date_before_8 = now - datetime.timedelta(days=8)
# 例如:获取现在之后12天的日期:
date_after_12 = now + datetime.timedelta(days=12)
# 例如:获取当前时间之前8小时:
date_before_8_hours = now - datetime.timedelta(hours=8)
# 分别获取datetime 的date和time对象:
now = datetime.datetime.now().
now.date()
now.time()
# 分别获取datetime的年月日:
now.year
now.month
now.day
# 获取两个datetime的时间差,返回天数:
(date1 - date2).days
标签:
原文地址:http://www.cnblogs.com/Raymon-Geng/p/5508978.html