python的内置模块time和datetime的方法详解以及使用(python内的time和datetime时间格式)
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
python的内置模块time和datetime的方法详解以及使用(python内的time和datetime时间格式)的更多相关文章
- 【转】Python的hasattr() getattr() setattr() 函数使用方法详解
Python的hasattr() getattr() setattr() 函数使用方法详解 hasattr(object, name)判断一个对象里面是否有name属性或者name方法,返回BOOL值 ...
- python eval() hasattr() getattr() setattr() 函数使用方法详解
eval() 函数 --- 将字符串str当成有效的表达式来求值并返回计算结果. 语法:eval(source[, globals[, locals]]) ---> value 参数: sour ...
- Python的循环正确的操作使用方法详解
要计算1+2+3,我们可以直接写表达式: >>> 1 + 2 + 3 6 要计算1+2+3+...+10,勉强也能写出来. 但是,要计算1+2+3+...+10000,直接写表达式就 ...
- Python的hasattr() getattr() setattr() 函数使用方法详解
hasattr(object, name)判断一个对象里面是否有name属性或者name方法,返回BOOL值,有name特性返回True, 否则返回False.需要注意的是name要用括号括起来 1 ...
- Python的hasattr() getattr() setattr() 函数使用方法详解 (转)
来自:https://www.cnblogs.com/cenyu/p/5713686.html hasattr(object, name)判断一个对象里面是否有name属性或者name方法,返回BOO ...
- Python的hasattr() getattr() setattr() 函数使用方法详解--转载
hasattr(object, name)判断一个对象里面是否有name属性或者name方法,返回BOOL值,有name特性返回True, 否则返回False.需要注意的是name要用括号括起来 1 ...
- python的匿名函数 lambda的使用方法详解以及使用案例
1.匿名函数是用lambda这个关键字定义 lambda x:x+1 第一个x代表形参,x+1相当于函数的返回值 #lambda x:x+1 第一个x代表形参,x+1相当于函数的返回值 def ...
- Python的Django框架中forms表单类的使用方法详解
用户表单是Web端的一项基本功能,大而全的Django框架中自然带有现成的基础form对象,本文就Python的Django框架中forms表单类的使用方法详解. Form表单的功能 自动生成HTML ...
- Python操作SQLite数据库的方法详解
Python操作SQLite数据库的方法详解 本文实例讲述了Python操作SQLite数据库的方法.分享给大家供大家参考,具体如下: SQLite简单介绍 SQLite数据库是一款非常小巧的嵌入式开 ...
随机推荐
- Beta周王者荣耀交流协会第一次Scrum会议
1.立会照片 成员王超,高远博,冉华,王磊,王玉玲,任思佳,袁玥全部到齐. master:王超 2.时间跨度: 2017年11月10日 15:10 — 15:50 ,总计40分钟. 3.地 点: 一食 ...
- 20145214 《网络对抗技术》 MSF基础应用
20145214 <网络对抗技术> MSF基础应用 1.实验后回答问题--用自己的话解释什么是exploit,payload,encode 如果把MSF比作一把枪的话,payload应该是 ...
- c# bitmap和new bitmap(bitmap)及在System.Drawing.Image.get_RawFormat()报错“参数无效”
问题情境: 给picturebox赋image属性,我用一下代码,出错: Bitmap theBitmap = convertCameraData.display(rawDataArray, heig ...
- 第一次spring冲刺第3、4天
11月14至15日 因为忙于项目的谈论与探究,以及周末的活动变动,使得博客没有时间提交上去,这里补交. 这两天,主要是应对于市场的探究做谈论,我们主要面对什么类型的客户,以及他们最需要的是什么等话题做 ...
- 【图论】POJ-3723 最大生成树
一.题目 Description Windy has a country, and he wants to build an army to protect his country. He has p ...
- 配置docker的私有仓库
1:安装docker-registry包 yum install -y docker-distribution 2:启动docker-distribution,默认监听于TCP/5000端口 sy ...
- Oculus VR眼镜调研
VR眼镜 oculus 软件安装 最详细的Oculus Rift VR/3D眼镜安装设置教程(多图) 在线安装Oculus rift驱动[非VPN方法] Rift,Go,Gear VR,Quest: ...
- Asp.net MVC area
妈的,今天去携程面试,技术面了三轮,竟然让我走了,没有然后了,你不要老子,干嘛还面那么多轮,害的老子一上午的时间没了,气死我了. 好了,总结下面试中的问题吧, 1.GC 2.设计模式 3.做过的项目的 ...
- [转帖]通俗解释 AWS 云服务每个组件的作用
你有听说过 ContainerCache,ElastiCast 和 QR72 这些 AWS 的新服务吗? 没有就对了,这些都是我编的:) 不过,AWS 有 50 多个服务,从名称也不能看出这些服务是做 ...
- [51CTO]反客为主 ,Linux 成为微软 Azure 上最流行的操作系统
反客为主 ,Linux 成为微软 Azure 上最流行的操作系统 [世界上唯一确定不变的就是世界在不停的变化] 三年前,微软云计算 Azure 平台 CTO Mark Russinovich 说有四分 ...