博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
time 模块
阅读量:6069 次
发布时间:2019-06-20

本文共 3817 字,大约阅读时间需要 12 分钟。

 

1. time模块

1.1测试

>>> time.time()1523778095.6930795>>> time.localtime()time.struct_time(tm_year=2018, tm_mon=4, tm_mday=15, tm_hour=15, tm_min=41, tm_sec=45, tm_wday=6, tm_yday=105, tm_isdst=0)>>> time.localtime().tm_year2018>>> time.localtime().tm_mon4

 

1.2转换

time.localtime() 可以传入参数, time.time()不行

>>> tt=time.time()>>> tl=time.localtime()>>> >>> time.localtime(tt)time.struct_time(tm_year=2018, tm_mon=4, tm_mday=15, tm_hour=17, tm_min=26, tm_sec=42, tm_wday=6, tm_yday=105, tm_isdst=0)>>> time.time(tl)Traceback (most recent call last):  File "
", line 1, in
time.time(tl)TypeError: time() takes no arguments (1 given) >>>

>>> time.mktime(tl)

1523784410.0

 

 

1.3格式化

可以对time.localtime()格式化,不能对time.time()格式化

>>> "%s-%s-%s %s:%s:%s"%(tl.tm_year,tl.tm_mon,tl.tm_mday,tl.tm_hour,tl.tm_min,tl.tm_sec)
>>> time.strftime('%Y-%m-%d %H:%M:%S',tl)'2018-04-15 17:26:50'>>> >>> time.strftime('%Y-%m-%d %H:%M:%S',tt)Traceback (most recent call last):  File "
", line 1, in
time.strftime('%Y-%m-%d %H:%M:%S',tt)TypeError: Tuple or struct_time argument required>>>

或者直接对当前时间格式化

>>> time.strftime('%Y-%m-%d %H:%M:%S')'2018-04-15 18:21:42'>>>

 

1.4 格式化与反格式化

>>> s1=time.strftime('%Y-%m-%d %H:%M:%S')>>> time.strptime(s1,'%Y-%m-%d %H:%M:%S')time.struct_time(tm_year=2018, tm_mon=4, tm_mday=15, tm_hour=18, tm_min=37, tm_sec=11, tm_wday=6, tm_yday=105, tm_isdst=-1)>>>

 

 1.5 全程

>>> tt=time.time()>>> tt1523789896.6532319>>> tl=time.localtime(tt)>>> time.strftime('%Y-%m-%d',tl)   #丢失了 小时 分钟 等信息'2018-04-15'>>> time.strptime('2018-04-15','%Y-%m-%d')time.struct_time(tm_year=2018, tm_mon=4, tm_mday=15, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=105, tm_isdst=-1)>>> tl2=time.strptime('2018-04-15','%Y-%m-%d')>>> time.mktime(tl2)1523721600.0>>>

 

 

 

1.6 帮助

>>> help(time.strftime)

Help on built-in function strftime in module time:

strftime(...)

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.

>>>

 

2 datetime模块

2.1测试

>>> >>> ddn=datetime.datetime.now()>>> ddndatetime.datetime(2018, 4, 15, 19, 3, 47, 798172)>>> ddn.year2018>>>

 

 2.2 转换

time.time 转换为datetime对象格式

>>> datetime.datetime.fromtimestamp(time.time())datetime.datetime(2018, 4, 15, 19, 9, 53, 623096)

 

2.3 格式化

>>> ddn=datetime.datetime.now()>>> ddn.strftime('%Y-%m-%d')'2018-04-15'>>> datetime.datetime.strftime(ddn,'%Y-%m-%d')'2018-04-15'

 

2.4 日期相差

>>> datetime.datetime.now()- datetime.timedelta(hours=3)datetime.datetime(2018, 4, 15, 16, 18, 19, 118009)>>> >>> datetime.datetime.now()- datetime.timedelta(days=1)datetime.datetime(2018, 4, 14, 19, 18, 24, 542319)>>>

 >>> datetime.datetime.now()- datetime.timedelta(minutes=30)

 datetime.datetime(2018, 4, 15, 18, 49, 5, 502662)
 >>>

 

 

 日期改写

>>> ddn=datetime.datetime.now()>>> ddndatetime.datetime(2018, 4, 15, 19, 20, 20, 565955)>>> ddn.replace(year=2016)datetime.datetime(2016, 4, 15, 19, 20, 20, 565955)>>>

 

转载于:https://www.cnblogs.com/infaaf/p/8847863.html

你可能感兴趣的文章
ASP.NET MVC中实现多个button提交的几种方法
查看>>
树与森林的存储、遍历和树与森林的转换
查看>>
CSS设计指南之浮动与清除
查看>>
Servlet3.0之八:基于Servlet3.0的文件上传@MultipartConfig
查看>>
adb shell am 的用法
查看>>
codeforces 85D D. Sum of Medians Vector的妙用
查看>>
Android进程的内存管理分析
查看>>
php -- 反射ReflectionClass
查看>>
Nginx反向代理和负载均衡部署指南
查看>>
java获取当前日期时间代码总结
查看>>
互联网广告学——程序化购买
查看>>
新版本chrome浏览器控制台怎么设置成独立的窗口
查看>>
oracle中nvarchar2字符集不匹配
查看>>
Mysql5.6.22源代码安装
查看>>
每天一个linux命令(5):rm 命令
查看>>
mksquash_lzma-3.2 编译问题
查看>>
【转帖】C# DllImport 系统调用使用详解 托管代码的介绍 EntryPoint的使用
查看>>
PHP JAVA Bridge桥的最新使用
查看>>
免费工具
查看>>
锋利的JQuery —— DOM操作
查看>>