python入门之time模块和datetime模块
time模块
时间三种表示:时间戳(秒单位),struct_time(元组,可以分开调用),指定格式(格式化)
time.sleep() 等待5秒钟
time.time() 返回时间戳
time.ctime() 返回当前系统的字符串时间
time.ctime(time.time()) 将时间戳转换为字符串格式
time.gmtime(time.time()) 将时间转换为struct_time格式,元组格式(国外时间,与本地相差12小时,UTC时区)
>>>time.struct_time(tm_year=2019, tm_mon=3, tm_mday=4, tm_hour=7, tm_min=17, tm_sec=6, tm_wday=0, tm_yday=63, tm_isdst=0)<<<
print(help(x)) 查看调用struct_time格式的方法
time.localtime(time.time()) 转换为struct_time格式,显示的是本地时间,UTC+8时区
time.mktime(time.localtime()) 与localtime相反,转换为时间戳
time.strftime("%Y-%m-%d %H:%M:%S,time.gmtime()") 将struct_time格式转换为指定的字符串格式
time.strptime("2017-01-01","%Y-%m-%d") 将字符串格式转换为struct_time格式
datetime模块
c_time = datetime.datetime.now() 当前时间(1)
print(c_time.timetuple()) 返回struct_time格式(2)
print(c_time.replace()) 返回当前时间,加入参数,如replace(,,),则替换时间(3)
(1)datetime.datetime(2019, 3, 4, 15, 42, 14, 241116)
(2)time.struct_time(tm_year=2019, tm_mon=3, tm_mday=4, tm_hour=15, tm_min=41, tm_sec=14, tm_wday=0, tm_yday=63, tm_isdst=-1)
(3)2019-03-04 15:41:14.967399
datetime.datetime.strptime("21/11/06 16:30","%d/%m/%y %H:%M") 将字符串转换为日期
new_date = datetime.datetime.now() + datetime.timedelta(days=) 比现在加10天
days=- 比现在减10天
hours=- 比现在减10小时
seconds= 比现在加10秒
weeks= 比现在加一周
python入门之time模块和datetime模块的更多相关文章
- 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 ...
- python中time模块和datetime模块
time模块和datetime模块 时间分为三种模式(time 模块) 时间戳 (time.time()) 格式化字符串 (time.strftime(%Y-%m-%d %H:%M:%S %p)) ...
- Time模块和datetime模块
Time模块和datetime模块 一. 调用 import time #调用time模块 二.使用方法 1.time.time 拿到时间戳.以Linux诞生年份1970年开始计算到程序执 ...
- 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时间元组,共 ...
- python笔记7 logging模块 hashlib模块 异常处理 datetime模块 shutil模块 xml模块(了解)
logging模块 日志就是记录一些信息,方便查询或者辅助开发 记录文件,显示屏幕 低配日志, 只能写入文件或者屏幕输出 屏幕输出 import logging logging.debug('调试模式 ...
- Python 标准类库-日期类型之datetime模块
标准类库-日期类型之datetime模块 by:授客 QQ:1033553122 可用类型 3 实践出真知 4 timedelta对象 4 class datetime.timedelta(da ...
随机推荐
- findBug 错误修改指南
1. EC_UNRELATED_TYPESBug: Call to equals() comparing different types Pattern id: EC_UNRELATED_TYPE ...
- MyBatis总结(1)
MyBatis前身是ibatis,是一个数据持久层框架.封装优化了普通JDBC过程, 如数据库连接的创建.设置SQL语句参数.执行SQL语句.事务.结果映射以及资源释放等. MyBatis是一个支持普 ...
- macOS 安装 Docker
系统要求 Docker for Mac 要求系统最低为 macOS 10.10.3 Yosemite,或者 2010 年以后的 Mac 机型,准确说是带 Intel MMU 虚拟化的,最低 4GB 内 ...
- 6、RNA-Seq Analysis Pipeline
Created by Dhivya Arasappan, last modified by Dennis C Wylie on Nov 08, 2015 This pipeline uses an a ...
- ZOJ 3939 The Lucky Week (暴力找规律)
题意:给定一个幸运日,求第 k 个幸运日是多少. 析:由于闰年,每400肯定会循环一次,所以我们就可以先找出每400年会有多少幸运日,是2058个,然后再暴力. 代码如下: #pragma comme ...
- Unity3d 脚本使用规则
脚本是Unity游戏开发的重要组成部分,通过脚本可以监听游戏中的相关事件和响应玩家的输入,并在游戏中安排事件发生.另外,脚本还可用于创建图形效果,控制对象的物理行为等.在Unity中使用脚本是需要注意 ...
- Windows10安装node.js,vue.js以及创建第一个vue.js项目
[工具官网] Node.js : http://nodejs.cn/ 淘宝NPM: https://npm.taobao.org/ 一.安装环境 1.本机系统:Windows 10 Pro(64位)2 ...
- 图像标注工具labelImg使用方法
最近在做打标签的工作,为了与大家参考学习,总结了在windows的环境下,基于anaconda的图像标注工具labellmg的一种使用方法! 1 搭建anaconda 以前写过怎么搭建anaconda ...
- ProtoBuf练习(二)
重复数据类型 protobuf语言的重复字段类型相当于C++的std::list数据类型 工程目录结构 $ ls proto/ TServer.proto TSession.proto proto文件 ...
- cinder介绍及使用lvm本地存储
1.cinder简介 Cinder提供持久的块存储,目前仅供给虚拟机挂载使用.它并没有实现对块设备的管理和实际服务,而是为后端不同的存储结构提供了统一的接口,不同的块设备服务厂商在 Cinder 中实 ...