python time,datetime

time

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

  • 时间戳 197011日之后的秒,既:time.time()

  • 格式化的字符串 2016-11-11 11:11 既:time.strftime('%Y-%m-%d')

  • 结构化时间元组包含了:年、日、星期等 time.struct_time,既: time.localtime()

>>> time.time()

1478586442.8984866

>>> time.localtime()

time.struct_time(tm_year=2016, tm_mon=11, tm_mday=8, tm_hour=14, tm_min=31, tm_sec=55, tm_wday=1, tm_yday=313, tm_isdst=0)

>>> time.gmtime()

time.struct_time(tm_year=2016, tm_mon=11, tm_mday=8, tm_hour=6, tm_min=34, tm_sec=6, tm_wday=1, tm_yday=313, tm_isdst=0)

>>> time.strftime('2016-11-11 14:32:35')

'2016-11-11 14:32:35'

>>> time.strptime('2016-11-11', '%Y-%m-%d')

time.struct_time(tm_year=2016, tm_mon=11, tm_mday=11, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=316, tm_isdst=-1)

>>> time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())

'2016-11-08 14:38:56'

>>> time.strptime('2016-11-11 14:32:35', '%Y-%m-%d %H:%M:%S')

time.struct_time(tm_year=2016, tm_mon=11, tm_mday=11, tm_hour=14, tm_min=32, tm_sec=35, tm_wday=4, tm_yday=316, tm_isdst=-1)

>>> time.asctime()

'Tue Nov 8 14:39:31 2016'

>>> time.asctime(time.localtime())

'Tue Nov 8 14:39:45 2016'

>>> time.ctime(time.time())

'Tue Nov 8 14:40:21 2016'

将时间对象转换为时间戳

>>> time.mktime(time.localtime())

1478587280.0

>>> '{year}-{month}'.format(year=time_obj.tm_year, month=time_obj.tm_mon)

'2016-11'

time.sleep(0.1)

datetime

timedelta

表示时间间隔,两个时间点的间隔。

A timedelta object represents a duration, the difference between two dates or times.

class datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)

timedelta(microseconds=1)

datetime.timedelta(0, 0, 1)

print(_)

0:00:00.000001

前一天的倒数第五小时

timedelta(hours=-5)

 datetime.timedelta(-1, 68400)

print(_)

-1 day, 19:00:00 

timedelta.min

datetime.timedelta(-999999999)

timedelta.max

datetime.timedelta(999999999, 86399, 999999)

timedelta.resolution

datetime.timedelta(0, 0, 1)

year = timedelta(weeks=40, days=84, hours=23, minutes=50, seconds=600)

year.total_seconds()

31536000.0

datetime

d = date(2005, 7, 14)

t = time(12, 30)

t = time(12, 30)

dt = datetime.combine(d, t)

print(dt)

2005-07-14 12:30:00


dt = datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M")

print(dt)

2006-11-21 16:30:00

 

tt = dt.timetuple()

print(tt)

time.struct_time(tm_year=2006, tm_mon=11, tm_mday=21, tm_hour=16, tm_min=30, tm_sec=0, tm_wday=1, tm_yday=325, tm_isdst=-1)

for it in tt:

     print(it)   

2006

11

21

16

30

0

1

325

-1 

ic = dt.isocalendar()

for it in ic:

     print(it)    

2006  #ISO year

47    #ISO week

2     #ISO weekday



print(dt)

2006-11-21 16:30:00

dt.strftime("%A, %d, %B %Y %I:%M%p")

'Tuesday, 21, November 2006 04:30PM'



>>> a = datetime.date.today()

>>> print(a)

2016-11-08

>>> a = datetime.datetime.now()

>>> print(a)

2016-11-08 14:58:46.357823

>>> a = datetime.datetime.now().timetuple()

>>> print(a)

time.struct_time(tm_year=2016, tm_mon=11, tm_mday=8, tm_hour=14, tm_min=59, tm_sec=40, tm_wday=1, tm_yday=313, tm_isdst=-1)

>>> a = datetime.datetime.now() + datetime.timedelta(days=2)

>>> print(a)

2016-11-10 15:00:29.025828

>>> a = datetime.datetime.now().replace(2015,11,8)

>>> print(a)

2015-11-08 15:01:30.680864

格式字符意义

%a星期的简写。如星期三为Web 
%A
星期的全写。如星期三为Wednesday 
%b
月份的简写。如4月份为Apr 
%B
月份的全写。如4月份为April 
%c:
日期时间的字符串表示。(如:04/07/10 10:43:39 
%d:
日在这个月中的天数(是这个月的第几天) 
%f:
微秒(范围[0,999999] 
%H:
小时(24小时制,[0, 23] 
%I:
小时(12小时制,[0, 11] 
%j:
日在年中的天数[001,366](是当年的第几天) 
%m:
月份([01,12] 
%M:
分钟([00,59] 
%p: AM
或者PM 
%S:
秒(范围为[00,61],为什么不是[00, 59],参考python手册~_~ 
%U:
周在当年的周数当年的第几周),星期天作为周的第一天 
%w:
今天在这周的天数,范围为[0, 6]6表示星期天 
%W:
周在当年的周数(是当年的第几周),星期一作为周的第一天 
%x:
日期字符串(如:04/07/10 
%X:
时间字符串(如:10:43:39 
%y: 2
个数字表示的年份 
%Y: 4
个数字表示的年份 
%z:
utc时间的间隔(如果是本地时间,返回空字符串) 
%Z:
时区名称(如果是本地时间,返回空字符串) 
%%: %% => % 

posted @ 2017-02-07 10:02  hexm  阅读(227)  评论(0编辑  收藏  举报
联系我:xiaoming.unix@gmail.com