Python提供两种时间表示方式,一种是时间戳,从1970年1月1日 0时开始。一种是struct_time数组格式,共有9个元素。
import time
print(time.time()) #返回时间戳,从1970年1月1日00:00:00开始。返回值为float。
print(time.ctime()) #输出当前系统时间。
print(time.ctime(time.time()-86400))#当前时间减去一天,返回字符串格式。
print(time.gmtime()) #返回数组形式的struct_time,共九个元素。为UTC时间
# tm_year, tm_mon, tm_mday, tm_hour, tm_min,tm_sec, tm_wday, tm_yday, tm_isdst
now = time.gmtime()
print(now.tm_year)
print(time.localtime()) #返回数组形式的struct_time,共九个元素。为本地时间
print(time.mktime(time.localtime())) #将struct_time转换成字符串格式的时间戳
print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime())) #将Struct_time格式转换为指定格式输出。UTC时间。
print(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())) #将Struct_time格式转换为指定格式输出。本地时间
time.sleep(4) #程序休眠4s
 
import datetime
print(datetime.date.today()) #返回格式:2016-06-07
print(datetime.date.fromtimestamp(time.time()))
print(datetime.datetime.now()) #返回当前时间2016-06-07 15:34:24.228779
print(datetime.datetime.now().timetuple()) #返回struct_time格式的时间
new_date = datetime.datetime.now() + datetime.timedelta(days=10) #比现在加10天
new_date = datetime.datetime.now() + datetime.timedelta(days=-10) #比现在减10天
new_date = datetime.datetime.now() + datetime.timedelta(hours=-10) #比现在减10小时
new_date = datetime.datetime.now() + datetime.timedelta(seconds=120) #比现在+120s
print(new_date)

Python内置模块-time && datatime的更多相关文章

  1. Python内置模块(re+collections+time等模块)

    Python内置模块(re+collections+time等模块) 1. re模块 import re 在python要想使用正则必须借助于模块 re就是其中之一 1.1 findall功能( re ...

  2. python内置模块(4)

    这一部分是python内置模块系列的最后一部分,介绍了一些小巧有用的内置模块. 目录: 1.random 2.shelve 3.getpass 4.zipfile 5.tarfile 6.bisect ...

  3. Python学习笔记【第八篇】:Python内置模块

    什么时模块 Python中的模块其实就是XXX.py 文件 模块分类 Python内置模块(标准库) 自定义模块 第三方模块 使用方法 import 模块名 form 模块名 import 方法名 说 ...

  4. Python内置模块与标准库

    Python内置模块就是标准库(模块)吗?或者说Python的自带string模块是内置模块吗? 答案是:string不是内置模块,它是标准库.也就是说Python内置模块和标准库并不是同一种东西. ...

  5. python内置模块[re]

    python内置模块[re] re模块: python的re模块(Regular Expression正则表达式)提供各种正则表达式的匹配操作,在文本解析.复杂字符串分析和信息提取时是一个非常有用的工 ...

  6. python内置模块[sys,os,os.path,stat]

    python内置模块[sys,os,os.path,stat] 内置模块是python自带功能,在使用内置模块时,需要遵循 先导入在 使用 一.sys 对象 描述 sys.argv 命令行参数获取,返 ...

  7. Python内置模块和第三方模块

    1.Python内置模块和第三方模块 内置模块: Python中,安装好了Python后,本身就带有的库,就叫做Python的内置的库. 内置模块,也被称为Python的标准库. Python 2.x ...

  8. python内置模块collections介绍

    目录 python内置模块collections介绍 1.namedtuple 2.deque 3.defaultdict 4.OrderedDict 5.ChainMap 6.Counter 7.小 ...

  9. Python标准库-datatime和time

    Python标准库-datatime和time 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.标准库datatime 1>.datatime模块 #!/usr/bin/e ...

随机推荐

  1. 使用nexus3.10搭建maven私有仓库

    使用nexus3.10搭建maven私有仓库-----详见如下链接-- --此贴用于笔记 https://blog.csdn.net/vipbupafeng/article/details/80232 ...

  2. react — script引入 和 脚手架使用的区别

    1.React 入门实例教程 :http://www.ruanyifeng.com/blog/2015/03/react.html ( 阮一峰的博客)  或   https://segmentfaul ...

  3. ZROI week3

    作业 poj 1091 跳蚤 容斥原理. 考虑能否跳到旁边就是卡牌的\(gcd\)是否是1,可以根据裴蜀定理证明. 考虑正着做十分的麻烦,所以倒着做,也就是用\(M^N - (不合法)\)即可. 不合 ...

  4. undefined null测试

    测试浏览器:chrome 当有父元素的子元素未定义时undefined和null均为true,类型为undefined 当元素赋给null后undefined和null均为true,类型为object ...

  5. thinkphp5 图片上传七牛云

    <?php namespace app\cxc\controller; use Qiniu\Auth; use Qiniu\Storage\UploadManager; use think\Co ...

  6. Excel表格文本格式的数字和数字格式如何批量转换

    Excel表格文本格式的数字和数字格式如何批量转换 在使用Excel表格对数据求和时,只能对单元格内常规格式的数据进行计算,而不能对单元格中的文本格式的数据进行计算,特点就是在单元格的左上角有一个绿色 ...

  7. PHP处理地址匹配出省市区

    function handleAddress($address ='广东省深圳市龙华新区大浪街道同胜科技大厦'){ preg_match('/(.*?(省|自治区|北京市|天津市))/', $addr ...

  8. Reactor 反应堆设计模式

    为了应对高并发的服务器端开发,微软在2009年提出了一种更优雅地实现异步编程的方式Reactive Programming即反应式编程.随后其他技术紧随其后,比如ES6通过引入类似的异步编程方式等. ...

  9. python学习笔记:循环语句——while、for

    python中有两种循环,while和for,两种循环的区别是,while循环之前,先判断一次,如果满足条件的话,再循环,for循环的时候必须有一个可迭代的对象,才能循环,比如说得有一个数组.循环里面 ...

  10. Dayjs处理时间函数的插件

    现在很多处理时间格式化的插件,我平时项目中最常用的便是Dayjs这个插件,我觉得这个插件还是很好用的.现在说一下这个插件的基本使用. Dayjs并没有覆盖Javascript原生的Date.proto ...