python时间模块time

#时间模块

  时间模块主要处理和时间相关的事件,我们可以通过模块获取不同数据类型的时间以便我们需求。

##表现时间的三种方式:

  在pythn中表现时间的方式主要有三种:时间戳(stamptime)、元祖时间(structtime)、格式化字符串时间,在大多数编程里面也有这些概念。

  1、时间戳时间:通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。返回的是float类型

  2、格式化字符串时间:列如:‘1992-12-1’

  3、元祖时间:该元祖共有9个元素(年,月,日,时,分,秒,一年中第几周,一年中第几天等)

###时间戳时间

  在使用time模块前记得导入模块,下面来看下如何获取时间戳,语法:time.time().

```

>>> import time
>>> time.time()
1541072964.2124543

```

  注意:time.time()不接受参数,返回是当前时间的时间戳时间,float型。

###字符串时间

  语法:time.strftime(format,p_tuple=None),该方法接受两个参数,一个是要显示时间的字符串格式,以及元祖时间,该参数为默认参数,不传则获取当前元祖时间来转化成字符串时间,以下是该方法的详细介绍:

```

def strftime(format, p_tuple=None): # real signature unknown; restored from __doc__
"""
strftime(format[, tuple]) -> string

Convert a time tuple to a string according to a format specification.
See the library reference manual for formatting codes. When the time tuple
is not present, current time as returned by localtime() is used.

Commonly used format codes:

%Y Year with century as a decimal number.
%m Month as a decimal number [01,12].
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%M Minute as a decimal number [00,59].
%S Second as a decimal number [00,61].
%z Time zone offset from UTC.
%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.
%I Hour (12-hour clock) as a decimal number [01,12].
%p Locale's equivalent of either AM or PM.

Other codes may be available on your platform. See documentation for
the C library strftime function.
"""
return ""

```

字符串格式中文对照表:

%y 两位数的年份表示(00-99)
%Y 四位数的年份表示(000-9999)
%m 月份(01-12)
%d 月内中的一天(0-31)
%H 24小时制小时数(0-23)
%I 12小时制小时数(01-12)
%M 分钟数(00=59)
%S 秒(00-59)
%a 本地简化星期名称
%A 本地完整星期名称
%b 本地简化的月份名称
%B 本地完整的月份名称
%c 本地相应的日期表示和时间表示
%j 年内的一天(001-366)
%p 本地A.M.或P.M.的等价符
%U 一年中的星期数(00-53)星期天为星期的开始
%w 星期(0-6),星期天为星期的开始
%W 一年中的星期数(00-53)星期一为星期的开始
%x 本地相应的日期表示
%X 本地相应的时间表示
%Z 当前时区的名称
%% %号本身

  

 

posted @ 2018-09-12 18:54  Kingfan  阅读(210)  评论(0编辑  收藏  举报