python学习-time,datetime模块

time 模块

时间相关的操作,时间有三种表示方式:

  • 时间戳               1970年1月1日之后的秒,即:time.time()
  • 格式化的字符串    2014-11-11 11:11,    即:time.strftime('%Y-%m-%d')
  • 结构化时间          元组包含了:年、日、星期等... time.struct_time    即:time.localtime()
 1 import time
 2 print(time.clock()) #返回处理器时间,3.3开始已废弃 , 改成了time.process_time()测量处理器运算时间,不包括sleep时间,不稳定,mac上测不出来
 3 print(time.altzone)  #返回与utc时间的时间差,以秒计算\
 4 print(time.asctime()) #返回时间格式"Mon Jun  5 15:17:54 2017",
 5 print(time.localtime()) #返回本地时间 的struct time对象格式
 6 print(time.gmtime(time.time()-800000)) #返回utc时间的struc时间对象格式
 7 print(time.asctime(time.localtime())) #返回时间格式"Mon Jun  5 15:43:02 2017",
 8 print(time.ctime()) #返回Mon Jun  5 15:43:02 2017 格式, 同上
 9 print(time.time())  #1970年1月1日之后的秒,可以再命令前后使用它相减,就是命令运行的时间(秒)
10 print(time.strftime('%Y-%m-%d %H:%M:%S'))   #格式化的字符串,返回2017-06-05 15:43:02

输出:

 1 D:\Programs\Python\Python35\python.exe "D:/script/P_script/chapter2/day2/time mod.py"
 2 3.0186811080370876e-07
 3 -32400
 4 Mon Jun  5 15:43:51 2017
 5 time.struct_time(tm_year=2017, tm_mon=6, tm_mday=5, tm_hour=15, tm_min=43, tm_sec=51, tm_wday=0, tm_yday=156, tm_isdst=0)
 6 time.struct_time(tm_year=2017, tm_mon=5, tm_mday=27, tm_hour=1, tm_min=30, tm_sec=31, tm_wday=5, tm_yday=147, tm_isdst=0)
 7 Mon Jun  5 15:43:51 2017
 8 Mon Jun  5 15:43:51 2017
 9 1496648631.756541
10 2017-06-05 15:43:51
 1 # 日期字符串 转成  时间戳
 2 string_2_struct = time.strptime("2017/06/05","%Y/%m/%d") #将日期字符串 转成 struct时间对象格式
 3 print(string_2_struct)
 4 # #
 5 struct_2_stamp = time.mktime(string_2_struct) #将struct时间对象转成时间戳
 6 print(struct_2_stamp)
 7 
 8 #将时间戳转为字符串格式
 9 print(time.gmtime(time.time())) #将utc时间戳转换成struct_time格式
10 print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()) ) #将utc struct_time格式转成指定的字符串格式
11 
12 输出:
13 time.struct_time(tm_year=2017, tm_mon=6, tm_mday=5, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=156, tm_isdst=-1)
14 1496592000.0
15 time.struct_time(tm_year=2017, tm_mon=6, tm_mday=5, tm_hour=8, tm_min=59, tm_sec=54, tm_wday=0, tm_yday=156, tm_isdst=0)
16 2017-06-05 08:59:54

DirectiveMeaningNotes
%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-06-05 17:03:14.898632
print(datetime.date.fromtimestamp(time.time()) )  # 时间戳直接转成日期格式 2017-06-05
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)) #时间替换

输出:

2017-06-05 17:03:14.898632
2017-06-05
2017-06-05 17:03:14.898632
2017-06-08 17:03:14.898632
2017-06-02 17:03:14.898632
2017-06-05 20:03:14.898632
2017-06-05 17:33:14.898632
2017-06-05 02:03:14.898632

 补充:以下来源于《Python cookbook》

时间的换算

from datetime import timedelta

a= timedelta(days=2,hours=6,seconds=1)
b= timedelta(hours=20.5)

c= a+b

print(c)
print(c.days)   #打印多少天(整数)
print(c.seconds)    #打印不足整日的时间,换算成秒
print(c.total_seconds())    #打印所有时间,换算成秒
print(c.total_seconds()/3600)   #换算成小时

输出:
3 days, 2:30:01
3
9001
268201.0
74.50027777777778

 

日期、时间的计算

from datetime import timedelta,datetime
a = datetime(2012, 9, 23,20,10,30,10)  #生成2012-09-23 20:10:30.000010格式的时间,分别为年月日时分秒微秒
print(a)
print(a + timedelta(days= 10))  #加10天
b = datetime(2012,10,10)    #生成2012-10-10 00:00:00
print(b)
print(b - a)    #日期相减
print((b-a).days)   #打印相减得出的日期
now = datetime.today()  #当前时间
print(now)
print(now + timedelta(minutes=10))  #当前时间加10分钟

输出
2012-09-23 20:10:30.000010
2012-10-03 20:10:30.000010
2012-10-10 00:00:00
16 days, 3:49:29.999990
16
2017-06-26 16:50:50.329180
2017-06-26 17:00:50.329180

 

注意:datetime会自动处理闰年

>>> import datetime
>>> a= datetime.datetime(2012,2,28)
>>> b= datetime.datetime(2012,3,1)
>>> b-a
datetime.timedelta(2)
>>> a= datetime.datetime(2013,2,28)
>>> b= datetime.datetime(2013,3,1)
>>> b-a
datetime.timedelta(1)
>>>

字符串、日期的转换

>>> a = '2017-06-26'

>>> datetime.datetime.strptime(a,'%Y-%m-%d')  #字符串转换成日期
datetime.datetime(2017, 6, 26, 0, 0)

>>> datetime.datetime.strftime(datetime.datetime.now(),'%Y-%m-%d %H:%M:%S')  #日期转换成要求格式的字符串
'2017-06-26 17:04:20'

datetime.datetime(2017, 6, 26, 0, 0)
>>> d.strftime('%Y-%m-%d')
'2017-06-26'
>>>

 

posted @ 2017-06-05 17:09  露似真珠月似弓  阅读(188)  评论(0编辑  收藏  举报