一、time模块

1、表示时间的三种方式

>>> import time

>>> time.time()  #当前时间戳
1509525556.8485825 >>> time.localtime() #用struct_time结构保存当前时间
time.struct_time(tm_year=2017, tm_mon=11, tm_mday=1, tm_hour=16, tm_min=39, tm_s
ec=24, tm_wday=2, tm_yday=305, tm_isdst=0) >>> time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) #用字符串形式格式化时间
'2017-11-01 16:39:38'

2、三种格式之间的相互转化

>>> time.gmtime(time.time()) #时间戳转化为struct_time
time.struct_time(tm_year=2017, tm_mon=11, tm_mday=1, tm_hour=8, tm_min=53, tm_se
c=51, tm_wday=2, tm_yday=305, tm_isdst=0) >>> time.mktime(time.localtime()) #struct_time转化为时间戳
1509526449.0 >>> time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) #struct_time转化为字符串
'2017-11-01 16:54:27' >>> time.strptime('2017-11-01', '%Y-%m-%d') #字符串转化为struct_time
time.struct_time(tm_year=2017, tm_mon=11, tm_mday=1, tm_hour=0, tm_min=0, tm_sec
=0, tm_wday=2, tm_yday=305, tm_isdst=-1)

3、time.sleep()表示间隔多少秒再继续

>>> while True:
print('hello')
time.sleep(1) #表示每隔1秒打印一次‘hello’

二、datetime模块

import datatime

print(datetime.datetime.now())  #获取当前日期和时间
print(datetime.datetime.now()+datetime.timedelta(3)) #3天后的时间
print(datetime.datetime.now()+datetime.timedelta(-3)) #3天前的时间

Python-time和datetime模块的更多相关文章

  1. Python处理时间 time && datetime 模块

    Python处理时间 time  &&  datetime 模块 个人整理,获取时间方式: import datetime import time #获取当前时间:Thu Nov 03 ...

  2. python time 和 datetime模块

    time模块 时间相关的操作,时间有三种表示方式: 时间戳               1970年1月1日之后的秒,即:time.time() 格式化的字符串    2014-11-11 11:11, ...

  3. python time 和 datetime 模块

    时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.我们运行“type(time.time())”,返回的是float类型. 格式化的时间字 ...

  4. python time、datetime模块

    时间的三种格式:1)时间戳 2)格式化的时间字符串 3)元组(struct_time):time.struct_time(tm_year=1970, tm_mon=5, tm_mday=23, tm_ ...

  5. python time 和 datetime 模块的简介

    时间处理 time 和 datetime import timeimport datetimeprint time.time() #时间戳显示为1508228106.49print time.strf ...

  6. python内建datetime模块

    datetime 获取当前日期和时间 from datetime import datetime now = datetime.now() print(now) datetime转换为timestam ...

  7. python使用datetime模块计算各种时间间隔的方法

    python使用datetime模块计算各种时间间隔的方法 本文实例讲述了python使用datetime模块计算各种时间间隔的方法.分享给大家供大家参考.具体分析如下: python中通过datet ...

  8. python中datetime模块

    Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供 的接口与C标准库time.h基本一致.相比于time模块 ...

  9. python datetime模块参数详解

    Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块,它提供 的接口与C标准库time.h基本一致.相比于time模块,datetime模块的接 ...

  10. python time模块和datetime模块详解

    一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共 ...

随机推荐

  1. 解决Input number 框能够能够输入eeeeee 的问题

    onKeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode)))" 在input  type="n ...

  2. C#知识点提要

    本篇博文主要对asp.net mvc开发需要撑握的C#语言知识点进行简单回顾,尤其是C# 3.0才有的一些C#语言特性.对于正在学asp.net mvc的童鞋,不防花个几分钟浏览一下.本文要回顾的C# ...

  3. tomcat局域网内发布html

    1. 保证tomcat装好,启动 验证:浏览器输入:localhost:8080,看到下面页面表示成功 2. 把html文件或包含html的目录拷到Apach安装目录的Root目录下 (例如:C:\P ...

  4. SVM 之 MATLAB 实现代码

    MATLAB 中 SVM 实现 直接上代码 main.m %% Initialize data clear, clc, close all; load('data.mat'); y(y == 0) = ...

  5. SpringBoot | 第二十八章:监控管理之Spring Boot Admin使用

    前言 上一章节,我们介绍了Actuator的使用,知道了可通过访问不同的端点路径,获取相应的监控信息.但使用后也能发现,返回的监控数据都是以JSON串的形式进行返回的,对于实施或者其他人员来说,不是很 ...

  6. CTSC/APIO2018 游记

    狗牌滚粗选手,此博客证明我去过...... CTSC 消失的源代码与消失的分数...... 我也不知道发生了什么....... APIO 旁边两位小哥太强了,心态完全炸裂,最后也滚粗了...... 回 ...

  7. bootstrap --datetimepicker之时间段选择

    文件引入 <script type="text/javascript" src="css/jquery-3.2.1.js"></script& ...

  8. 关于如何绕开对通用VMware虚拟机检测的一些收集

    1,用记事本打开虚拟系统镜像文件的配置文件,这个文件扩展名为vmx,比如我的虚拟系统名为XP,那这个文件就叫XP.vmx,然后在其末尾添加这么一句,如下红色部分(注意,虚拟机不能在运行状态添加) mo ...

  9. 使用Access作数据库

    import java.sql.*; public class ConnectAccess { public static void main(String args[]){ ConnectAcces ...

  10. PHP Primary script unknown 终极解决方法

    相信很多配置php环境的都遇到过这个恼人的问题: 浏览器访问php文件,返回来 File not found 查看/var/log/nginx/error.log ,有 “Primary script ...