import datetime
import time
datetime_dt = datetime.datetime.today() # 获取当前日期和时间, 2017-10-26 10:03:28.693198
datetime_str = datetime_dt.strftime("%Y/%m/%d %H:%M:%S") # 将datetime 对象转换为字符串 , '2017/10/26 10:03:28'
表 15-1 strftime()指令
strftime 指令 含义
%Y 带世纪的年份,例如'2014'
%y 不带世纪的年份,'00'至'99'(1970至 2069)
%m 数字表示的月份, '01'至'12'
%B 完整的月份,例如'November'
%b 简写的月份,例如'Nov'
%d 一月中的第几天,'01'至'31'
%j 一年中的第几天,'001'至'366'
%w 一周中的第几天,'0'(周日)至'6'(周六)
%A 完整的周几,例如'Monday'
%a 简写的周几,例如'Mon'
%H 小时(24 小时时钟),'00'至'23'
%I 小时(12 小时时钟),'01'至'12'
%M 分,'00'至'59'
%S 秒,'00'至'59'
%p 'AM'或'PM'
%% 就是'%'字符
print(datetime_str)
time_delta = datetime.timedelta(hours=-3) # 时间段, -1 day, 21:00:00
datetime_pre = datetime_dt + time_delta # 将时间提前12小时, 2017-10-26 07:03:28.693198
print(datetime_pre.ctime())
# 将日期时间转为日期
date = datetime_dt.date() # 2017-10-26
print("现在是 {}年 {}月 {}日".format(date.year, date.month, date.day))
# 将日期时间转为时间
time = datetime_dt.time() # 10:03:28.693198
print("现在是 {}".format(time.isoformat()))
# 将日期时间转为时间戳
time_s = datetime_dt.timestamp() # 1508983408.693198
print("现在的时间戳: {}".format(time_s))
'''
用于处理日期和时间
'''
timedelta_temp = datetime.timedelta(seconds=60, minutes=59, hours=23) # 1 day, 0:00:00
date_temp = datetime.date.today() - datetime.timedelta(1) # 2017-10-25
# === timedelta ===
# 内部只存储: days日[-999999999, 999999999) / seconds秒 [0, 86399) / microseconds微秒(1E-6秒)[0, 999999)
# milliseconds毫秒(1‰秒)[ == 1000 * microseconds ] / minutes分[ == 60 * seconds] / hours时[ == 3600 * seconds] / weeks周期[ == 7 * days]
# timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0) // 表示时间的间隔
timedelta = datetime.timedelta(1) # 1 day, 0:00:00
time_total = timedelta.total_seconds() # 计算时间段的总秒数 ( => 86400.0 )
delta = datetime.timedelta(days=11, hours=10, minutes=9, seconds=8)
delta.total_seconds()
986948.0
str(delta)
'11 days, 10:09:08'
# 计算
boolean = timedelta == timedelta_temp # 时间是否相等 => True
timedelta = timedelta + timedelta_temp # 加上 => 2 days, 0:00:00
timedelta = timedelta - timedelta_temp # 减去 => 1 day, 0:00:00
timedelta = timedelta * 10 # 乘以 , 10 days, 0:00:00
timedelta = timedelta * 0.01 # 乘以浮点数 , 2:24:00
fnum = timedelta / timedelta_temp # 除以 , 0.1
timedelta = timedelta / 10 # 除以整数 或 浮点数 (不能除以0) => 0:14:24
num = timedelta // timedelta_temp # 商(delta / 整数)(不能除以0) => 0
timedelta = timedelta % timedelta_temp # 余数 => 0:14:24
num, time_delta = divmod(timedelta, timedelta_temp) # (商, 余数) => (0, 0:14:24)
timedelta = +timedelta # 内存地址引用(没用) 0:14:24
timedelta = -timedelta # 取反 => -1 day, 23:45:36
timedelta = abs(time_delta) # 绝对值 0:14:24
strs = str(time_delta) # 格式化为[D day[s], ][H]H:MM:SS[.UUUUUU]字符串 => '0:14:24'
strs = repr(time_delta) # 格式化为datetime.timedelta(D[, S[, U]])字符串 => 'datetime.timedelta(0, 864)'
# === date ===
# date 可做字典的键, 所有date对象被为True
# year年[MINYEAR, MAXYEAR] / month月[1, 12] / day日[1, 给定年月的最大天数]
# date(year, month, day) # 公历日历中的日期, 两个方向上无限延伸
date = datetime.date(2017, 5, 6) # 2017-05-06
date = date.min # 最早日期 => 0001-01-01
date = date.max # 最晚日期 => 9999-12-31
timedelta = date.resolution # 不相等日期之间最小差异 1 day, 0:00:00
date_year = date.year # year年 9999
date_month = date.month # month月 12
date_day = date.day # day日 [1, 给定年月的最大天数] 31
date = datetime.date.today() # 当前本地日期 (类方法) => 2017-10-26
date = datetime.date.fromtimestamp(time.time()) # 将时间戳转为日历 (类方法) => 2017-10-26
date = datetime.date.fromordinal(12) # 返回公历序数日期 (类方法) => 0001-01-12
# 计算
date = date + timedelta # 加 => 0001-01-13
date = date - timedelta # 0001-01-12
timedelta = date - timedelta # 日历相减 => 0001-01-11
# replace(year=None, month=None, day=None)
date = date.replace(day=12) # 替换 => 0001-01-12
num = date.toordinal() # date在公历日历中的序数 (从date.min开始数) 12
num = date.weekday() # 周几 (0:周日) 4
num = date.isoweekday() # 周几 (1:周一) 5
year, week, weekday = date.isocalendar()
# 返回年, 周数, 周几 (year, week, weekday) (注:weekday == isoweekday)
# 1,2,5,
date_str = date.isoformat() # 格式化为‘YYYY-MM-DD’格式 => '0001-01-12'
date_str = date.ctime() # 格式化为日期格式 => 'Fri Jan 12 00:00:00 0001' (注:时分秒均为0, 下同)
date_str = date.strftime("%Y-%m-%d-%H-%M-%S") # 格式化为指定格式 '0001-01-12-00-00-00'
# === datetime ===
# datetime 可做字典的键。所有datetime都为True
# year年[MINYEAR, MAXYEAR] / month月[1, 12] / day日[1, 给定年月的最大天数] / hour时[0, 24) / minute[0. 60) / second秒[0, 60) / microsecond微秒[0, 1000000)
# datetime.datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None)
# 类方法
date_time = datetime.datetime.today() # 当前本地日期时间 => 2017-10-26 10:23:24.661344
date_time = datetime.datetime.now() # 同today 2017-10-26 10:23:42.565845
date_time = datetime.datetime.utcnow() # UTC日期和时间 2017-10-26 02:23:49.267422
date_time = datetime.datetime.fromtimestamp(time.time()) # 时间戳转为datetime 2017-10-26 10:23:55.766708
date_time = datetime.datetime.utcfromtimestamp(time.time()) # 时间戳转为UTCdatetime 2017-10-26 02:24:00.511246
date_time = datetime.datetime.fromordinal(12) # 公历序数日期 0001-01-12 00:00:00
# 将字符串转换成 datetime对象
datetime.datetime.strptime('October 21, 2015', '%B %d, %Y')
# datetime.datetime(2015, 10, 21, 0, 0)
datetime.datetime.strptime('2015/10/21 16:29:00', '%Y/%m/%d %H:%M:%S')
# datetime.datetime(2015, 10, 21, 16, 29)
datetime.datetime.strptime("October of '15", "%B of '%y")
# datetime.datetime(2015, 10, 1, 0, 0)
datetime.datetime.strptime("November of '63", "%B of '%y")
# datetime.datetime(2063, 11, 1, 0, 0)
# 实例方法
date = date_time.date() # 转为date => 2008-01-01
time = date_time.time() # 转为time => 00:00:00
time_s = date_time.timestamp() # 转为时间戳 1199116800.0
struct_time = date_time.timetuple() # 转为 struct_time
struct_time = date_time.utctimetuple() # time.struct_time(tm_year=2008, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=1, tm_isdst=-1)
# replace([year[, month[, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]]]]]) // 替换
date_time = date_time.replace(year=2017) # =>2017-01-01 00:00:00
num = date_time.toordinal() # 公历序数 736330
num = date_time.weekday() # 周几 (0:周日) 6
num = date_time.isoweekday() # 周几 (1:周一) 7
year, week, weekday = date_time.isocalendar() # (year, week, weekday(1:星期一))2016,52,7
datetime_str = date_time.isoformat(sep='T') # 格式化, sep时间和日期分隔符 => '2017-01-01T00:00:00'
datetime_str = date_time.ctime() # 格式化 'Sun Jan 1 00:00:00 2017'
datetime_str = date_time.strftime("%Y-%m-%d-%H-%M-%S") # 格式化 '2017-01-01-00-00-00'