二十三、python中的time和datetime模块
A、time模块
1. sleep():强制等待
import time
import datetime print("start to sleep.....")
time.sleep(5)
print("wake up ...........")
'''
2.time.time():当前系统的时间戳
'''
print(time.time())
---------------------------
1533021214.0050707
---------------------------
'''
3.time.ctime():当前系统时间
'''
print (time.ctime())
print(time.ctime(time.time()-86400))#昨天的此刻
---------------------------
Tue Jul 31 15:13:34 2018
Mon Jul 30 15:13:34 2018
---------------------------
'''
4.time.gmtime():将时间转化成struct_time格式
'''
print (time.gmtime())
time_obj=time.gmtime(time.time()-86400) #昨天的此刻(将时间戳转化成struct_time格式)
print (time_obj)
print (str(time_obj.tm_year)+"-"+str(time_obj.tm_mon)+"-"+str(time_obj.tm_mday))
print("%s-%s-%s %s:%s:%s" %(time_obj.tm_year,time_obj.tm_mon,time_obj.tm_mday,time_obj.tm_hour,time_obj.tm_min,time_obj.tm_sec))
---------------------------
time.struct_time(tm_year=2018, tm_mon=7, tm_mday=31, tm_hour=7, tm_min=32, tm_sec=9, tm_wday=1, tm_yday=212, tm_isdst=0)
time.struct_time(tm_year=2018, tm_mon=7, tm_mday=30, tm_hour=7, tm_min=32, tm_sec=9, tm_wday=0, tm_yday=211, tm_isdst=0)
2018-7-30
2018-7-30 7:32:9
---------------------------
'''
4.1.字符串格式化(扩展)
%s:字符串
%d:整型
%f:浮点型
'''
time_obj=time.gmtime(time.time()-86400) #昨天的此刻
print("%s-%s-%s %s:%s:%s" %(time_obj.tm_year,time_obj.tm_mon,time_obj.tm_mday,time_obj.tm_hour,time_obj.tm_min,time_obj.tm_sec))
---------------------------
2018-7-30 7:32:9
---------------------------
'''
5.localtime():将时间戳转化成struct_time格式,返回本地的时间
'''
print (time.localtime(time.time()-86400))
---------------------------
time.struct_time(tm_year=2018, tm_mon=7, tm_mday=31, tm_hour=14, tm_min=51, tm_sec=20, tm_wday=1, tm_yday=212, tm_isdst=0)
---------------------------
'''
6.time.mktime(time.localtime()):将struct_time转化成时间戳格式
'''
print (time.mktime(time.localtime()))
---------------------------
1533106558.0
---------------------------
'''
7.time.strptime():将字符串格式日期转化成struct_time格式
''' print (time.strptime("2018-08-01 15:02","%Y-%m-%d %H:%M"))
---------------------------
time.struct_time(tm_year=2018, tm_mon=8, tm_mday=1, tm_hour=15, tm_min=2, tm_sec=0, tm_wday=2, tm_yday=213, tm_isdst=-1)
---------------------------
'''
8.time.strftime():将时间对象转成指定的字符串格式
'''
print (time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()))
print (time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()))
---------------------------
2018-08-01 07:05:10
2018-08-01 15:05:10
---------------------------
'''
9.time.asctime():返回时间格式"Fri Aug 19 11:14:16 2016
'''
print(time.asctime())
---------------------------
Wed Aug 1 15:06:53 2018
---------------------------
B、datatime
'''
1.datetime.date.today():打印当天的日期,格式:2018-08-01
'''
print (datetime.date.today())
---------------------------
2018-08-01
---------------------------
'''
2.datetime.date.fromtimestamp(time.time()):将时间戳转化成日期格式
'''
print (datetime.date.fromtimestamp(time.time()))
---------------------------
2018-08-01
---------------------------
'''
3.datetime.datetime.now():返回当前时间,时分秒
datetime.datetime.now().timetuple():将当前时间转化成struct_time格式
'''
current_time=datetime.datetime.now()
print(current_time)
print(current_time.timetuple())
---------------------------
2018-08-01 15:20:18.638428
time.struct_time(tm_year=2018, tm_mon=8, tm_mday=1, tm_hour=15, tm_min=20, tm_sec=18, tm_wday=2, tm_yday=213, tm_isdst=-1)
---------------------------
'''
4.datetime.datetime.now().replace(yyyy,mm,dd):将当前时间替换成指定时间
datetime.datetime.now().replace():指定格式为空的话返回当前日期
' '
current_time=datetime.datetime.now()
print(current_time.replace())
print(current_time.replace(2066,12,12))
---------------------------
2018-08-01 15:27:29.508072
2066-12-12 15:27:29.508072
---------------------------
'''
5.时间的加减
'''
print (datetime.datetime.now()+datetime.timedelta(days=10)) #比现在加10天
print (datetime.datetime.now()-datetime.timedelta(days=10)) #比现在减10天
print (datetime.datetime.now()+datetime.timedelta(hours=2)) #比现在加10小时
print (datetime.datetime.now()-datetime.timedelta(seconds=10)) #比现在减10秒
print (datetime.datetime.now()-datetime.timedelta(minutes=10)) #比现在减10分钟
print (datetime.datetime.now()+datetime.timedelta(weeks=1)) #比现在加一周
---------------------------
2018-08-11 15:56:19.873044
2018-07-22 15:56:19.873044
2018-08-01 17:56:19.873044
2018-08-01 15:56:09.873044
2018-08-01 15:46:19.873044
2018-08-08 15:56:19.873044
---------------------------
二十三、python中的time和datetime模块的更多相关文章
- python3.4学习笔记(二十三) Python调用淘宝IP库获取IP归属地返回省市运营商实例代码
python3.4学习笔记(二十三) Python调用淘宝IP库获取IP归属地返回省市运营商实例代码 淘宝IP地址库 http://ip.taobao.com/目前提供的服务包括:1. 根据用户提供的 ...
- 二十三. Python基础(23)--经典类和新式类
二十三. Python基础(23)--经典类和新式类 ●知识框架 ●接口类&抽象类的实现 # 接口类&抽象类的实现 #①抛出异常法 class Parent(object): ...
- 数理统计(二)——Python中的概率分布API
数理统计(二)——Python中的概率分布API iwehdio的博客园:https://www.cnblogs.com/iwehdio/ 数理统计中进行假设检验需要查一些分布的上分位数表.在scip ...
- Python第十五天 datetime模块 time模块 thread模块 threading模块 Queue队列模块 multiprocessing模块 paramiko模块 fabric模块
Python第十五天 datetime模块 time模块 thread模块 threading模块 Queue队列模块 multiprocessing模块 paramiko模块 fab ...
- Python中操作mysql的pymysql模块详解
Python中操作mysql的pymysql模块详解 前言 pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而后者不支持 ...
- python模块 re模块与python中运用正则表达式的特点 模块知识详解
1.re模块和基础方法 2.在python中使用正则表达式的特点和问题 3.使用正则表达式的技巧 4.简单爬虫例子 一.re模块 模块引入; import re 相关知识: 1.查找: (1)find ...
- python中的计时器:timeit模块
python中的计时器:timeit模块 (1) timeit - 通常在一段程序的前后都用上time.time()然后进行相减就可以得到一段程序的运行时间,不过python提供了更强大的计时库:ti ...
- python2.7高级编程 笔记二(Python中的描述符)
Python中包含了许多内建的语言特性,它们使得代码简洁且易于理解.这些特性包括列表/集合/字典推导式,属性(property).以及装饰器(decorator).对于大部分特性来说,这些" ...
- python学习(二)python中的核心数据类型
数据类型是编程语言中的很重要的一个组成部分,我所知道的有数据类型的好处有:在内存中存放的格式知道,规定了有哪几种可用的操作. 我的埋点:为什么要有数据类型 那么python中的数据类型有哪几种呢? 对 ...
随机推荐
- Paper Reading_Computer Architecture
最近(以及预感接下来的一年)会读很多很多的paper......不如开个帖子记录一下读paper心得 Computer Architecture Last level cache (llc) perf ...
- ASP.NET服务器控件Menu
http://www.cnblogs.com/huc87/archive/2009/04/05/1429831.html ASP.NET服务器控件Menu 1. ASP.NET 服务器控件 ...
- react搭建
https://juejin.im/post/5b4de4496fb9a04fc226a7af
- Java List对象集合按对象属性分组、分组汇总、过滤等操作示例
import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Col ...
- iOS手势操作,拖动,轻击,捏合,旋转,长按,自定义(http://www.cnblogs.com/huangjianwu/p/4675648.html)
1.UIGestureRecognizer 介绍 手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性. iOS 系统在 3.2 以后,他提供了一些常用的手势(UIGestureReco ...
- python的java胶水(jpype1)
1.直接使用pip安装jpype1 命令 pip install jpype1 但是,很不幸,提示报错,缺少VC++组件. 2.使用其他方法安装 在 https://www.lfd.uci.edu/ ...
- zookeeper之二 zkCli客户端命令
ZooKeeper命令行界面(CLI)用于与ZooKeeper集合进行交互以进行开发.它有助于调试和解决不同的选项.要执行ZooKeeper CLI操作,首先打开ZooKeeper服务器(“bin/z ...
- overflow:hidden失效问题
2018-08-03 Questions about work 这几天开发的时候遇到了个问题,如图1. 写了个demo demo 地址 由于页面并没有进行整体缩放,导致在小屏幕手机上显示会有异常.PM ...
- 查看jar包内容
查看jar包内容 查看jar包内容的基本命令: jar tf jar-file 参数解释: The t option indicates that you want to view the table ...
- pyqt5-QPlainTextEdit普通文本
继承:QAbstractScrollArea QPlainText和QTextEdit大致功能实现差不多,不能有图片.框架.表格等 QPlainText是安行滚动的,QTextEdit是安像素滚动的 ...