Day5模块-time和datetime模块
模块是封装一段代码来实现某种功能。
分为三类:
1.自定义模块
2.标准库,内置模块
3.开源模块
-----------------------------------------------------------------------------------------------------
1.time模块
>>> import time
#返回处理器时间,3.3后变成了time.process_time()测量处理器运算时间,不包括sleep时间,不稳定
>>> print(time.clock())
2.9933526190766363e-06
>>> print(time.process_time())
0.09360059999999999 #返回与utc时间的时间差,以秒计算\
>>> print(time.altzone)
-32400
#本地时间
>>> print(time.localtime())
time.struct_time(tm_year=2017, tm_mon=5, tm_mday=21, tm_hour=20, tm_min=54, tm_s
ec=47, tm_wday=6, tm_yday=141, tm_isdst=0)
#返回utc时间的struc时间对象格式
>>> print(time.gmtime(time.time()-800000))
time.struct_time(tm_year=2017, tm_mon=5, tm_mday=12, tm_hour=6, tm_min=41, tm_se
c=56, tm_wday=4, tm_yday=132, tm_isdst=0)
#返回时间格式
>>> print(time.asctime())
Sun May 21 20:56:26 2017
>>> print(time.asctime(time.localtime()))
Sun May 21 20:55:55 2017
日期字符串转换为时间戳
#将字符串转换成struct对象
>>> string_2_struct = time.strptime("2016/05/22","%Y/%m/%d")
>>> print(string_2_struct)
time.struct_time(tm_year=2016, tm_mon=5, tm_mday=22, tm_hour=0, tm_min=0, tm_sec
=0, tm_wday=6, tm_yday=143, tm_isdst=-1)
>>>
#将struct对象转换为时间戳对象
>>> string_2_stamp = time.mktime(string_2_struct)
>>> print(string_2_stamp)
1463846400.0
时间戳转换为字符串
>>> print(time.time()) #当前utc时间,时间是秒
1495372102.3229373
>>> print(time.gmtime()) #struct对象的时间格式
time.struct_time(tm_year=2017, tm_mon=5, tm_mday=21, tm_hour=13, tm_min=8, tm_se
c=29, tm_wday=6, tm_yday=141, tm_isdst=0) #将utc时间戳转换成struct_time格式
>>> print(time.gmtime(time.time()-86640))
time.struct_time(tm_year=2017, tm_mon=5, tm_mday=20, tm_hour=13, tm_min=1, tm_se
c=42, tm_wday=5, tm_yday=140, tm_isdst=0)
#将utc struct_time格式转成指定的字符串格式
>>> print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()))
2017-05-21 13:07:45
时间加减
>>> import datetime
>>> print("datetime".center(50,'*'))
*********************datetime*********************
>>> print(datetime.datetime.now()) #返回当前时间
2017-05-21 21:12:10.507988
# 时间戳直接转成日期格式 2016-08-19
>>> print(datetime.date.fromtimestamp(time.time()))
2017-05-21 #返回当前时间
>>> print(datetime.datetime.now())
2017-05-21 21:17:05.163842
#时间戳直接转成日期格式 2016-08-19
>>> print(datetime.date.fromtimestamp(time.time()))
2017-05-21
#当前时间加3天
>>> print(datetime.datetime.now()+datetime.timedelta(3))
2017-05-24 21:17:39.092782
#当前时间减3天
>>> print(datetime.datetime.now()+datetime.timedelta(-3))
2017-05-18 21:17:46.039180
#当前时间加3小时
>>> print(datetime.datetime.now()+datetime.timedelta(hours=3))
2017-05-22 00:18:01.337055
#当前时间加30分钟
>>> print(datetime.datetime.now()+datetime.timedelta(minutes=30))
2017-05-21 21:49:16.833373
时间替换
>>> c_time = datetime.datetime.now()
>>> print(c_time.replace(minute=3,hour=2))
2017-05-21 02:03:05.940477
>>> print(datetime.datetime.now)
<built-in method now of type object at 0x0000000067D262C0>
>>> print(datetime.datetime.now())
2017-05-21 21:23:49.137948
| 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. |
字符串、时间格式、utc时间的转换关系

a.字符串转换为utc时间
>>> time.strptime("2016/05/22","%Y/%m/%d")
time.struct_time(tm_year=2016, tm_mon=5, tm_mday=22, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=143, tm_isdst=-1)
>>> struct_time = time.strptime("2016/05/22","%Y/%m/%d")
>>> time.mktime(struct_time)
1463846400.0
b.utc时间转换为字符串
>>> time.gmtime(1463846400.0)
time.struct_time(tm_year=2016, tm_mon=5, tm_mday=21, tm_hour=16, tm_min=0, tm_se
c=0, tm_wday=5, tm_yday=142, tm_isdst=0)
>>> time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime())
'2017-05-21 13:30:47'
Day5模块-time和datetime模块的更多相关文章
- python常用模块之time&datetime模块
python常用模块之time&datetime模块 在平常的代码中,我们经常要与时间打交道.在python中,与时间处理有关的模块就包括:time和datetime,下面分别来介绍: 在开始 ...
- 模块学习-time,datetime模块
1 time.timezone #以秒为单位显示时区 >>> import time >>> time.timezone -28800 北京为东八区,所以为-288 ...
- Day5 - Python基础5 常用模块学习
Python 之路 Day5 - 常用模块学习 本节大纲: 模块介绍 time &datetime模块 random os sys shutil json & picle shel ...
- Python 常用模块之time&datetime 和random
本节大纲: 模块介绍 time &datetime模块 random 一.模块介绍: 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他 ...
- Python时间日期格式化之time与datetime模块总结
1 引言 在实际开发过程中,我们经常会用到日期或者时间,那么在Python中我们怎么获取时间,以及如何将时间转换为我们需要的格式呢?在之前的开发中,也曾遇到time.datetime等模块下的不同函数 ...
- time&datetime模块
在Python中,和时间处理相关的模块有time,datatime,calendar(不常用)三个. UTCC(Coordinated Universal Time,世界协调时)亦即格林威治天文时间, ...
- day 5 模块导入、常用模块os shutil sys commands subprocess hashlib json pickle zipfile traceback random datetime
os: os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录:相当于shell下cd os. ...
- Python时间日期格式化之time与datetime模块
1 引言 在实际开发过程中,我们经常会用到日期或者时间,那么在Python中我们怎么获取时间,以及如何将时间转换为我们需要的格式呢?在之前的开发中,也曾遇到time.datetime等模块下的不同函数 ...
- 第十章、datetime模块
目录 第十章.datetime模块 一.datetime 模块 第十章.datetime模块 一.datetime 模块 import datetime#返回当前时间 print(datetime.d ...
随机推荐
- Laravel查询构造器的使用方法整理
1.结果集 1.1从一张表获取所有行,get方法获取所有行 $users = DB::table('users')->get(); 获取列的值 foreach ($users as $user) ...
- 老李分享:《Linux Shell脚本攻略》 要点(六)
老李分享:<Linux Shell脚本攻略> 要点(六) 1.打印网络接口列表 [root@localhost touch_more]# ifconfig | cut -c-10 | ...
- c++标准库容器【转】
C++最原始的容器之一是数组.数组的特点有: 1.大小固定 2.单独存在的数组建立在栈上,作为对象成员存在的数组建立在堆上还是栈上则要看作为宿主对象是被建立在堆上还是栈上.栈空间是有限的,所以如果数组 ...
- 透视I/O多路复用
透视I/O多路复用 我写的不是select这些函数的教学,需要了解的请自行Google或者去man,这些是帮助我理解函数的封装之下的道理. 需要回答的问题 I/O准备好了指什么?什么叫I/O已经可读/ ...
- JavaScript的XMLHttpRequest对象
ajax的核心技术是XMLHttpRequest对象,简称XHR. //支持IE7及更高的版本 function createXML() { if(typeof XMLHttpReqquest!=&q ...
- CSAcademy Beta Round #3 a-game
题目连接 a-game 大意:有一个只包含A和B的字符串,两个人分别取这个串的子串,但是每一次取不能与之前取到过的子串有交集,最后谁取到的所有串中A的总数量少的判为胜.如果一样,则为平手. 给出这样的 ...
- 八种创建等高列布局【出自w3c】
高度相等列在Web页面设计中永远是一个网页设计师的需求.如果所有列都有相同的背景色,高度相等还是不相等都无关紧要,因为你只要在这些列的父元素中设置一个背景色就可以了.但是,如果一个或多个列需要单独设置 ...
- Linux系统操作指令汇总
1.系统配置 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIO ...
- Python-3------新年考试周的Python学习
2016一开始就是考试周,准备专业课的考试复习.每天上午复习,晚上复习到8点半,之后到10点这点时间来看Python.庆幸没有在忙碌的时候荒废 Python的学习. 期待寒假,以前寒假在家总是没事做, ...
- bzoj1798 [Ahoi2009]维护序列
Description 老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成. 有长为N的数列,不妨设为a1,a2,…,aN .有如下三种操作形式: (1)把数列中的一段数全部乘一个值; (2 ...