Heim  >  Artikel  >  Backend-Entwicklung  >  利用python获得时间的实例说明

利用python获得时间的实例说明

WBOY
WBOYOriginal
2016-06-06 11:27:561435Durchsuche

代码如下:


import time 

print time.time() 

print time.localtime(time.time()) 

print time.strftime('%Y-%m-%d', time.localtime()) 

print time.strftime('%y-%m-%d', time.localtime()) 

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

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

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

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

下面是效果:

代码如下:


1295099282.16
time.struct_time(tm_year=2011, tm_mon=1, tm_mday=15, tm_hour=21, tm_min=48, tm_sec=2, tm_wday=5, tm_yday=15, tm_isdst=0)   
2011-01-15
11-01-15
2011-01-15 21:48:02
2011-01-15 09:48:02
2011-01-15 21:48:02 --Saturday--01/15/11 21:48:02
2011-01-15 21:48:02 --01/15/11--21:48:02

下面是time.strftime具体的参数说明:

time.strftime里面有很多参数,可以让你能够更随意的输出自己想要的东西:
下面是time.strftime的参数:
strftime(format[, tuple]) -> string
将指定的struct_time(默认为当前时间),根据指定的格式化字符串输出
python中时间日期格式化符号:
%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 当前时区的名称
%% %号本身

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn