python中与time模块和datetime模块相关操作
使用time模块计时程序运行时长 import time
time_start = time.time() #程序主体部分 time_end = time.time()
print('总耗时: ', time_end-time_start, '秒')
python 日期、时间、字符串相互转换
参考https://www.cnblogs.com/huhu-xiaomaomi/p/10338472.html
在python中,日期类型date和日期时间类型dateTime是不能比较的。
(1)如果要比较,可以将dateTime转换为date,date不能直接转换为dateTime
import datetime
dateTime_p = datetime.datetime.now()
date_p = dateTime_p.date()
print(dateTime_p) #2019-01-30 15:17:46.573139
print(date_p) #2019-01-30
(2)日期类型date转换为字符串str
#!/usr/bin/env python3
import datetime
date_p = datetime.datetime.now().date()
str_p = str(date_p)
print(date_p,type(date_p)) #2019-01-30 <class 'datetime.date'>
print(str_p,type(str_p)) #2019-01-30 <class 'str'>
(3)字符串类型str转换为dateTime类型
import datetime
str_p = '2019-01-30 15:29:08'
dateTime_p = datetime.datetime.strptime(str_p,'%Y-%m-%d %H:%M:%S')
print(dateTime_p) # 2019-01-30 15:29:08 >>> a='2019-08-15 16:01:20'
>>> a
'2019-08-15 16:01:20'
>>> import datetime
>>> datetime.datetime.strptime(a,'%Y-%m-%d %H:%M:%S')
datetime.datetime(2019, 8, 15, 16, 1, 20)
>>> b= datetime.datetime.strptime(a,'%Y-%m-%d %H:%M:%S')
>>> b
datetime.datetime(2019, 8, 15, 16, 1, 20)
>>> print(b)
2019-08-15 16:01:20
>>>
(4)dateTime类型转为str类型
这个地方我也不太理解,为什么指定格式无效
import datetime
dateTime_p = datetime.datetime.now()
str_p = datetime.datetime.strftime(dateTime_p,'%Y-%m-%d')
print(dateTime_p) # 2019-01-30 15:36:19.415157
(5)字符串类型str转换为date类型
#!/usr/bin/env python3
import datetime
str_p = '2019-01-30'
date_p = datetime.datetime.strptime(str_p,'%Y-%m-%d').date()
print(date_p,type(date_p)) # 2019-01-30 <class 'datetime.date'>
另外dateTime类型和date类型可以直接做加1减1这种操作
#!/usr/bin/env python3
import datetime
# today = datetime.datetime.today()
today = datetime.datetime.today().date()
yestoday = today + datetime.timedelta(days=-1)
tomorrow = today + datetime.timedelta(days=1)
print(today) # 2019-01-30
print(yestoday)# 2019-01-29
print(tomorrow)# 2019-01-31
python中与time模块和datetime模块相关操作的更多相关文章
- python中time模块和datetime模块
time模块和datetime模块 时间分为三种模式(time 模块) 时间戳 (time.time()) 格式化字符串 (time.strftime(%Y-%m-%d %H:%M:%S %p)) ...
- Python之路(第十六篇)xml模块、datetime模块
一.xml模块 xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单, xml比较早,早期许多软件都是用xml,至今很多传统公司如金融行业的很多系统的接口还主要 ...
- (转)python time模块和datetime模块详解
python time模块和datetime模块详解 原文:http://www.cnblogs.com/tkqasn/p/6001134.html 一.time模块 time模块中时间表现的格式主要 ...
- python time模块 sys模块 collections模块 random模块 os模块 序列化 datetime模块
一 collections模块 collections模块在内置的数据类型,比如:int.str.list.dict等基础之上额外提供了几种数据类型. 参考博客 http://www.pythoner ...
- Time模块和datetime模块
Time模块和datetime模块 一. 调用 import time #调用time模块 二.使用方法 1.time.time 拿到时间戳.以Linux诞生年份1970年开始计算到程序执 ...
- Python 中的时间处理包datetime和arrow
Python 中的时间处理包datetime和arrow 在获取贝壳分的时候用到了时间处理函数,想要获取上个月时间包括年.月.日等 # 方法一: today = datetime.date.today ...
- python字符串、字符串处理函数及字符串相关操作
python字符串.字符串处理函数及字符串相关操作 字符串介绍 python字符串表示 Python除处理数字外还可以处理字符串,字符串用单撇号或双撇号包裹: >>> 'spam e ...
- python time模块和datetime模块详解
一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共 ...
- python time模块和datetime模块
一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共 ...
随机推荐
- docker 详细安装及问题排查
一.安装docker 1.Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker . 通过 uname -r 命令查看 ...
- 面试总结【css篇】- css选择器以及优先级
优先(优先级为): !important > 内联样式 > #id > .class > tag > * > 继承 > 默认 . 当选择器的权重相同时,它将 ...
- MySQL在win10以及linux下数据库的备份以及还原
MySQL在win环境或者linux下的命令都是一样的,只是路径不一致而已 MySQL的备份 (非必须)命令行进入MySQL的bin目录 输入命令:mysqldump -u userName -p d ...
- 【HDUOJ】1257 最少拦截系统
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1257 题意:经典题. 题解:最长上升子序列. 代码: #include <iostream> ...
- CentOS 7 用 yum 安装 Nginx
在 CentOS 7 中,直接使用 yum 安装 Nignx 会提示无下载源.因此,需要添加 Nginx 的下载源到 yum: sudo rpm -Uvh http://nginx.org/packa ...
- python的magic methods
https://pycoders-weekly-chinese.readthedocs.io/en/latest/issue6/a-guide-to-pythons-magic-methods.htm ...
- PHP72w安装
PHP72w # rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm # rpm ...
- pxe装机试验 2019.8.21
部署FTP服务 1.安装FTP服务,并将安装源复制到/var/ftp/centos7目录下: [root@pxe ~]# yum -y install vsftpd [root@pxe ~]# mkd ...
- 47. List中特有的方法
集合的体系:--------------| Collection 单列集合的根接口----------| List 如果实现了List接口的集合类,该类具备的特点是:有序,可重复---------- ...
- delphi xe10 获取屏幕截图
//截取屏幕图片 function MakeScaleScreenshot(Sender: TControl): TBitmap; function GetScreenScale: Single; v ...