首页 > 代码库 > time和datetime模块
time和datetime模块
time.altzone #返回与UTC时间的时间差
time.time() #时间戳,1970年到现在的时间,以秒计算,时间戳可以对时间进行运算。
time.asctime() #返回时间格式"Sun May 14 13:40:11 2017",不加对象默认为当前时间,如:time.asctime(time.localtime(time.time()+3600*3)),当前时间往后推迟3小时,这种格式一般美国人在用
time.ctime() #返回当前时间‘Sun May 14 14:33:00 2017‘
time.localtime() #返回本地时间 的struct time对象格式,time.struct_time(tm_year=2017, tm_mon=5, tm_mday=14, tm_hour=14, tm_min=3, tm_sec=0, tm_wday=6, tm_yday=134, tm_isdst=0)
可以进行时间运算,如
time.localtime(time.time()+3600*3) #往后推迟3小时
time.gmtime() #返回UTC时间的struct time对象格式,time.struct_time(tm_year=2017, tm_mon=5, tm_mday=14, tm_hour=6, tm_min=3, tm_sec=0, tm_wday=6, tm_yday=134, tm_isdst=0)
time.strptime(string,format)#把字符串的时间格式 格式化输出一个struct_time
time.strftime(format,struct_time) #把struct_time对象格式化输出一个字符串的时间格式
如:
time.strptime(‘2017-5-1 23:59:59‘,‘%Y-%m-%d %H:%M:%S‘) ->time.struct_time(tm_year=2017, tm_mon=5, tm_mday=1, tm_hour=23, tm_min=59, tm_sec=59, tm_wday=0, tm_yday=121, tm_isdst=-1)
time.mktime() #将时间对象转成时间戳
#日期字符串转成时间戳
如:把一个字符串格式为‘2017-5-1 23:59:59‘转换成时间戳
t = time.strptime(‘2017-5-1 23:59:59‘,‘%Y-%m-%d %H:%M:%S‘) #先把字符串转为为struct-time格式
print(time.mktime(t)) ->1493654399.0 #再用mktime函数转换为时间戳
#将时间戳转换为字符串格式
如:
t = time.strptime(‘2017-5-1 23:59:59‘,‘%Y-%m-%d %H:%M:%S‘)
t1_stamp = time.mktime(t) #得到时间戳
t2 = time.localtime(t1_stamp) #时间戳转换为struct-time格式
t3 = time.strftime(‘%Y-%m-%d %H-%M-%S‘,t2) #转换为想要的时间字符串格式
print(t3) ->2017-05-01 23-59-59
时间格式
Directive | Meaning | Notes |
---|---|---|
%a |
Locale’s abbreviated weekday name. | |
%A |
Locale’s full weekday name. | |
%b |
Locale’s abbreviated month name. | |
%B |
Locale’s full month name. | |
%c |
Locale’s appropriate date and time representation. | |
%d |
Day of the month as a decimal number [01,31]. | |
%H |
Hour (24-hour clock) as a decimal number [00,23]. | |
%I |
Hour (12-hour clock) as a decimal number [01,12]. | |
%j |
Day of the year as a decimal number [001,366]. | |
%m |
Month as a decimal number [01,12]. | |
%M |
Minute as a decimal number [00,59]. | |
%p |
Locale’s equivalent of either AM or PM. | (1) |
%S |
Second as a decimal number [00,61]. | (2) |
%U |
Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. | (3) |
%w |
Weekday as a decimal number [0(Sunday),6]. | |
%W |
Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. | (3) |
%x |
Locale’s appropriate date representation. | |
%X |
Locale’s appropriate time representation. | |
%y |
Year without century as a decimal number [00,99]. | |
%Y |
Year with century as a decimal number. | |
%z |
Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59]. | |
%Z |
Time zone name (no characters if no time zone exists). | |
%% |
A literal ‘%‘ character. |
datetime模块
时间运算
import datetime print(datetime.datetime.now()) #返回当前时间2017-05-14 15:50:19.881892 print(datetime.date.fromtimestamp(time.time()) ) # 时间戳直接转成日期格式 2016-08-19,好像没什么用 print(datetime.datetime.now() ) print(datetime.datetime.now() + datetime.timedelta(3)) #当前时间+3天 print(datetime.datetime.now() + datetime.timedelta(-3)) #当前时间-3天 print(datetime.datetime.now() + datetime.timedelta(hours=3)) #当前时间+3小时 print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #当前时间+30分 c_time = datetime.datetime.now()
print(c_time.replace(minute=3,hour=2)) #时间替换
time和datetime模块