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

yipeiwu_com6年前Python基础

复制代码 代码如下:

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 当前时区的名称
%% %号本身

相关文章

Windows下anaconda安装第三方包的方法小结(tensorflow、gensim为例)

Windows下anaconda安装第三方包的方法小结(tensorflow、gensim为例)

 anaconda 集成了很多科学计算中所需要的包,如numpy,scipy等等,具体查看anaconda中已经预先安装配置好的包有哪些,可以通过cmd命令,输入conda l...

pandas 缺失值与空值处理的实现方法

pandas 缺失值与空值处理的实现方法

1.相关函数 df.dropna() df.fillna() df.isnull() df.isna() 2.相关概念 空值:在pandas中的空值是"" 缺失值:在...

python实现矩阵乘法的方法

本文实例讲述了python实现矩阵乘法的方法。分享给大家供大家参考。具体实现方法如下: def matrixMul(A, B): res = [[0] * len(B[0]) f...

Python中关键字global和nonlocal的区别详解

Python中关键字global和nonlocal的区别详解

前言 终于下定决心学习Python了。既然从头开始,就需要认认真真。 首先需要说的是,我是初学Python,这篇文章只是用于展示global和nonlocal关键字的区别,是很简单的知识...

使用python编写批量卸载手机中安装的android应用脚本

该脚本的功能是卸载android手机中安装的所有第三方应用,主要是使用adb shell pm、adb uninstall 命令,所以使用的前提是需要配好adb的环境变量,下面上代码:...