Python常用模块-时间模块(time&datetime)
Python常用模块-时间模块(time & datetime)
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.初始time模块
#!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:y1053419035@qq.com import time """"
模块的分类:
模块本质就是一个“*.py”文件,大致分为以下三类:
1>.内置模块,指的是存在Python解释器内部的模块,如time模块;
2>>第三方模块,指的是安装好Python后的lib文件夹中的模块;
3>.自定义模块,指的是你自己写的Python程序;
""" print(time.time()) #返回当前的时间戳,表示从1971年1月1日"00:00:00"到此刻时间节点的秒数。 s = time.localtime() #创建一个时间对象,也可以说是在结构化时间对象,返回本地时间的struct_time对象格式。
print(s)
print(s.tm_year) #获取年份。
print(s.tm_mon) #获取月份。 s2 = time.gmtime() #返回utc时间的struc时间对象格式。
print(s2) #以上代码执行结果如下:
1520176127.9244497
time.struct_time(tm_year=2018, tm_mon=3, tm_mday=4, tm_hour=23, tm_min=8, tm_sec=47, tm_wday=6, tm_yday=63, tm_isdst=0)
2018
3
time.struct_time(tm_year=2018, tm_mon=3, tm_mday=4, tm_hour=15, tm_min=8, tm_sec=47, tm_wday=6, tm_yday=63, tm_isdst=0)
二.时间模块的相互转换
1.转换助记图
2.案例展示
#!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:y1053419035@qq.com import time s = time.localtime(31245244545) #结构化时间对象,可以将时间戳转换成结构化时间。
print(s) s2 = time.mktime(time.localtime()) #将结构化时间转换成时间戳。
print(s2) s3 = time.strftime("%Y-%m-%d",time.localtime()) #将结构化时间转换成字符串时间。
print(s3) s4 = time.strptime("1993:05:19","%Y:%m:%d") #将字符串时间转换成结构化时间。
print(s4) #以上代码执行结果如下:
time.struct_time(tm_year=2960, tm_mon=2, tm_mday=15, tm_hour=2, tm_min=35, tm_sec=45, tm_wday=4, tm_yday=46, tm_isdst=0)
1520176194.0
2018-03-04
time.struct_time(tm_year=1993, tm_mon=5, tm_mday=19, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=139, tm_isdst=-1)
三.time扩充
#!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:y1053419035@qq.com import time
print(time.asctime(time.localtime())) #将结构化时间转换成字符串时间。
print(time.ctime(565656446)) #将时间戳转换成字符串时间。 time.sleep(2) #让程序暂停2秒钟,用于模拟I/O阻塞,并不占用CPU资源。 #以上代码执行结果如下:
Sun Mar 4 23:11:16 2018
Sat Dec 5 06:47:26 1987
四.time模块小试牛刀
#!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:y1053419035@qq.com import time s = "2018-03-04" def ChangeTime(string,number,format="%Y-%m-%d"):
StructTime = time.strptime(string,format )
print(StructTime)
StampTime = time.mktime(StructTime)
print(StampTime)
NewStamp = StampTime + 3600 * 24 * number
NewStringTime = time.strftime(format, time.localtime(NewStamp))
print(NewStringTime) ChangeTime(s,3) #推算3日后的时间 #以上代码执行结果如下:
time.struct_time(tm_year=2018, tm_mon=3, tm_mday=4, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=63, tm_isdst=-1)
1520092800.0
2018-03-07
五.datetime介绍
#!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:y1053419035@qq.com
import time,datetime print(datetime.datetime.now()) #打印当前系统时间 print(datetime.date.fromtimestamp(time.time())) #时间戳直接转成日期格式如:2018-03-04 print(datetime.datetime.now() + datetime.timedelta(3)) #当前时间+3天 print(datetime.datetime.now() + datetime.timedelta(-3)) #当前时间-3天 print(datetime.datetime.now() + datetime.timedelta(hours=3)) #当前时间+3小时 print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #当前时间+30分 c_time = datetime.datetime.now()
print(c_time.replace(minute=3,hour=2)) ##时间替换 #以上代码执行结果如下:
2018-03-04 23:12:13.477416
2018-03-04
2018-03-07 23:12:13.477416
2018-03-01 23:12:13.477416
2018-03-05 02:12:13.477416
2018-03-04 23:42:13.477416
2018-03-04 02:03:13.477416
六.关于格式化参数的详细说明
Directive | Meaning | Notes |
---|---|---|
%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. | |
%d |
Day of the month as a decimal number [01,31]. | |
%H |
Hour (24-hour clock) as a decimal number [00,23]. | |
%I |
Hour (12-hour clock) as a decimal number [01,12]. | |
%j |
Day of the year as a decimal number [001,366]. | |
%m |
Month as a decimal number [01,12]. | |
%M |
Minute as a decimal number [00,59]. | |
%p |
Locale’s equivalent of either AM or PM. | (1) |
%S |
Second as a decimal number [00,61]. | (2) |
%U |
Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. | (3) |
%w |
Weekday as a decimal number [0(Sunday),6]. | |
%W |
Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. | (3) |
%x |
Locale’s appropriate date representation. | |
%X |
Locale’s appropriate time representation. | |
%y |
Year without century as a decimal number [00,99]. | |
%Y |
Year with century as a decimal number. | |
%z |
Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59]. | |
%Z |
Time zone name (no characters if no time zone exists). | |
%% |
A literal '%' character. |
Python常用模块-时间模块(time&datetime)的更多相关文章
- Python常用内建模块
Python常用内建模块 datetime 处理日期和时间的标准库. 注意到datetime是模块,datetime模块还包含一个datetime类,通过from datetime import da ...
- 模块-时间模块(new)
模块-时间模块 导入: import time 方法: _STRUCT_TM_ITEMS __doc__ __loader__ __name__ __package__ __spec__ altzon ...
- Python 中的时间处理包datetime和arrow
Python 中的时间处理包datetime和arrow 在获取贝壳分的时候用到了时间处理函数,想要获取上个月时间包括年.月.日等 # 方法一: today = datetime.date.today ...
- Python常用模块-时间模块
在写代码的过程中,我们常常需要与时间打交道,在python中,与时间处理有关的模块有time,datetime和calendar.,这里主要介绍time和datetime模块 在python中,表示时 ...
- python内建时间模块 time和datetime
时间模块 UTC(Coordinated Universal Time,世界协调时)亦即格林威治天文时间,世界标准时间.在中国为UTC+8.DST(Daylight Saving Time)即夏令时. ...
- python常用内建模块——datetime
datetime是python处理日期和时间的标准库. 获取当前日期和时间 >>>from datetime import datetime >>>now = da ...
- python常用内建模块--datetime
datetime模块中的datetime类: 获取当前时间:datetime.now() 当前操作系统时区时间,date.utctime(UTC时间) 转换成时间戳:timestamp() 和具体时区 ...
- python常用函数及模块
原文来源于博客园和CSDN 1.计算函数 abs()--取绝对值 max()--取序列最大值,包括列表.元组 min()--取序列最小值 len()--取长度 divmod(a,b)---取a//b除 ...
- python的time时间模块
模块概述 1.一个.py文件就是一个模块 2.通过import语句在一个模块中导入另一个模块,import sys,print (sys.path),sys.path的结果为一个列表,列表的第一个元素 ...
随机推荐
- CS229笔记:线性回归
线性回归问题 首先做一些符号上的说明: \(x^{(i)}\):特征(feature) \(y^{(i)}\):目标变量(target variables) \(\mathcal{X}\):特征空间 ...
- 2、Docker镜像和镜像管理
一.镜像介绍 1.定义 一个只读层被称为镜像,一个镜像是永久不会变的. 由于 Docker 使用一个统一文件系统,Docker 进程认为整个文件系统是以读写方式挂载的. 但是所有的变更都发生顶层的可写 ...
- 你应该知道Go语言的几个优势
要说起GO语言的优势,我们就得从GO语言的历史讲起了-- 本文由腾讯技术工程官方号发表在腾讯云+社区 2007年,受够了C++煎熬的Google首席软件工程师Rob Pike纠集Robert Grie ...
- Mistakes I Made(as a developer)...大龄程序员的忠告...(部分转...)
在2006年,我开始了编程工作.当意识到来到了十年这个重要的时间关口时,我觉得有必要回顾一下这十年间所犯下的错误,做一做经验总结,并且给正在这个职业上奋斗的人们提出我的一些忠告.开发行业变化得很快,我 ...
- BugPhobia开发篇章:Beta阶段第II次Scrum Meeting
0x01 :Scrum Meeting基本摘要 Beta阶段第二次Scrum Meeting 敏捷开发起始时间 2015/12/13 00:00 A.M. 敏捷开发终止时间 2015/12/14 22 ...
- 20135119_涂文斌 实验三 敏捷开发与XP实践
北京电子科技学院(BESTI) 实 验 报 告 课程: Java 班级:1351 姓名:涂文斌 学号:20135119 成绩: ...
- process.tar.gz
exec1.c #include <stdio.h> #include <unistd.h> int main() { char *arglist[3]; arglist[0] ...
- 审评(HelloWorld团队)
炸弹人:我觉得炸弹人的构想很不错,很像以前玩的qq堂,不过上课时讲的不够深入,我没有找到项目的思路,项目的介绍也很粗糙,后面说的目标很大,希望你可以实现,我觉得越多的功能,就意味着越多的工作量,总的来 ...
- 第四次WBS
分解原则 1.将主体目标逐步细化分解,最底层的日常活动可直接分派到个人去完成: 2.每个任务原则上要求分解到不能再细分为止: 3.日常活动要对应到人.时间和资金投入. 二.任务分解的方法 1.采用树状 ...
- jdk命令行工具:jstat与jmap
转自文章:http://blog.csdn.net/gzh0222/article/details/8538727 C:\Users\Administrator\Desktop>jstat -g ...