time内置模块的方法

1、time() 时间戳

time() -> floating point number  浮点数
Return the current time in seconds since the Epoch.
Fractions of a second may be present if the system clock provides them.

import time
print(time.time()) C:\python35\python3.exe D:/pyproject/day21模块/time模块.py 1528517838.7509072

这个时间戳是一个秒数,是从1970年凌晨开始算,到现在一共经历了多少秒

现在是2018年,减去1970年,是48年,

48*365*24*60*60=1513728000

哈哈算出来跟上面的差不多,这就是时间戳,每一秒都不一样

时间戳可以可以用来计算2个时间的减法,就是比如我下单的时候是一个时间戳,我支付成功再来一个时间戳,可以计算一下我下单到支付花了多少秒

2、localtime(seconds=None) 结构化时间-当地时间
得到的是一个结构化时间

Convert seconds since the Epoch to a time tuple expressing local time.
When 'seconds' is not passed in, convert the current time instead

import time
print(time.localtime()) C:\python35\python3.exe D:/pyproject/day21模块/time模块.py time.struct_time(tm_year=2018, tm_mon=6, tm_mday=9, tm_hour=12, tm_min=36, tm_sec=7, tm_wday=5, tm_yday=160, tm_isdst=0)

那么我们就可以取出来具体的其中的具体的年份或者是时分秒,一周的第几天,一年的第几天

import time
a=time.localtime()
print(a.tm_year,a.tm_mon,a.tm_mday,a.tm_hour,":",a.tm_min,":",a.tm_sec) C:\python35\python3.exe D:/pyproject/day21模块/time模块.py 2018 6 9 12 : 46 : 1
3、gmtime  也是结构化时间 世界标准化时间-UTC

时间标准时间,跟我们的时间差8个小时

4、mktime(p_tuple)

mktime(tuple) -> floating point number

将结构化时间转换成时间戳

import time
print(time.mktime(time.localtime())) C:\python35\python3.exe D:/pyproject/day21模块/time模块.py 1528522939.0

5、strftime(format, p_tuple=None)

将结构化时间转化成字符串时间

%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. 

%X就代表时分秒

import time
print(time.strftime("%Y-%m-%d %X",time.localtime())) C:\python35\python3.exe D:/pyproject/day21模块/time模块.py 2018-06-09 15:11:04
6、strptime(string, format)

将字符串时间转化为结构化时间

%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.

这里的字符串时间得和后面的结构化时间一一对应才行

import time
print(time.strptime("2018:06:09-15:21:36","%Y:%m:%d-%X")) C:\python35\python3.exe D:/pyproject/day21模块/time模块.py time.struct_time(tm_year=2018, tm_mon=6, tm_mday=9, tm_hour=15, tm_min=21, tm_sec=36, tm_wday=5, tm_yday=160, tm_isdst=-1)
7、asctime(p_tuple=None)可以加结构化参数,不加参数默认是当前时间

如果你没有自定义需求时间格式的话,可以之间用这个方法

Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.
When the time tuple is not present, current time as returned by localtime()
is used

import time
print(time.asctime()) C:\python35\python3.exe D:/pyproject/day21模块/time模块.py Sat Jun 9 15:26:45 2018
8、ctime(seconds=None)可以加时间戳参数,不加参数默认是当前时间

跟7这个asctime出来的格式是一样的

print(time.ctime())#不加参数,默认是当前时间

Sat Jun  9 15:34:30 2018

print(time.ctime(1228629586.2798274))#加上时间戳(字符串时间)参数

Sun Dec  7 13:59:46 2008

9、datetime  这个相对来说跟好用,第一种用法比较精细,第二种格式也比较好看

import datetime
print(datetime.datetime.now())
print(datetime.datetime.now().strftime("%Y-%m-%d %X")) C:\python35\python3.exe D:/pyproject/day21模块/time模块.py 2018-06-09 15:44:29.870926 2018-06-09 15:44:29

最新文章

  1. BZOJ1572 工作安排 USACO2009
  2. Don't make a promise when you are in Joy. Don't reply when you are Sad.Don't take decisions when you are Angry.Think Twice.Act Wise.
  3. [SAP ABAP开发技术总结]EXIT-COMMAND
  4. 新建android项目src和layout文件夹中没有内容的问题
  5. PureMVC(JS版)源码解析:总结
  6. ∑–△型模数转换器(ADC)简介
  7. 【POJ1195】【二维树状数组】Mobile phones
  8. RxJava漫谈-RxAndroid使用
  9. (原创) ubuntu 12.04 install nvidia by the deb
  10. scrapy使用crontab定时任务不能自动执行的调试
  11. 基于Vivado调用ROM IP core设计DDS
  12. IntelliJ IDEA2018.3 最新破解方法
  13. eclipse中解决git分支合并冲突
  14. 329 experience
  15. 巧克力分配问题——C语言
  16. f5 Syslog管理
  17. 博客转移至github
  18. 委托事件和jquery中的delegate方法
  19. Metro应用Json数据处理
  20. <数据挖掘导论>读书笔记2

热门文章

  1. web.xml配置文件中<async-supported>true</async-supported>报错
  2. 软件功能说明书beta修订
  3. (第十一周)Beta—review阶段成员贡献分
  4. java实验三实验报告
  5. 【CSAPP笔记】11. 存储器层次结构
  6. java异常处理及自定义异常的使用
  7. 404 Note Found 团队会议纪要
  8. 父元素如果为none,子元素也是看不到的
  9. Beta版本测试第二天
  10. nowcoder 203J Graph Coloring I(dfs)