时间格式

一个名词:

UTC(Coordinated Universal Time):格林威治天文时,世界标准时间。中国为东八区 UTC+8

在编程的世界中,一定要了解的几种时间格式:

1、时间戳

  从 1970-1-1 00:00:00 开始按秒计算的浮点型偏移量。

2、格式化的时间字符串

  例如:2018-11-1 11:35:00

3、元组形式(struct_time)的时间,在python中包含9个元素

  形式: time.struct_time(tm_year=2018, tm_mon=10, tm_mday=31, tm_hour=18, tm_min=30, tm_sec=39, tm_wday=2, tm_yday=304, tm_isdst=0)

  含义:0--年,1--月,2--日,3--时,4--分,5--秒,6--周几(取值0~6,0表示周日),7--一年中的第几天,8--是否夏令时

time 模块

1、time.sleep(secs)        线程推迟指定时间运行,很常用。

2、time.time()                 返回当前时间戳

 >>>import time
>>>time.time()
1541043502.846

3、time.localtime([secs])       time.gmtime([secs])         参数默认传入time.time(),将时间戳转换为 struct_time ,前者返回当地时间,后者返回标准时间。

 >>>time.localtime()
time.struct_time(tm_year=2018, tm_mon=11, tm_mday=1, tm_hour=11, tm_min=47, tm_sec=49, tm_wday=3, tm_yday=305, tm_isdst=0)
>>>time.gmtime()
time.struct_time(tm_year=2018, tm_mon=11, tm_mday=1, tm_hour=3, tm_min=47, tm_sec=56, tm_wday=3, tm_yday=305, tm_isdst=0)

4、time.mktime(struct_time)          将 struct_time 转换为时间戳

 >>>time.mktime(time.localtime())
1541044257.0

5、time.strftime(format[, struct_time])           将 struct_time 转换为 格式化的时间字符串,struct_time 默认 time.localtime()

 >>>time.strftime('%Y-%m-%d %X')
'2018-11-01 11:58:08'

6、time.strptime(string[, format])                   将 格式化的时间字符串转换为 struct_time

 >>> time.strptime('2018-11-1 12:01', '%Y-%m-%d %H:%M')
time.struct_time(tm_year=2018, tm_mon=11, tm_mday=1, tm_hour=12, tm_min=1, tm_sec=0, tm_wday=3, tm_yday=305, tm_isdst=-1)

7、time.asctime([struct_time])         time.ctime([secs])             前者将 struct_time , 后者将时间戳 转换为 这种格式:'Thu Nov  1 12:07:27 2018'

格式化的时间字符串有个对应关系表,如 %M 表示分钟  %m 表示月份 ,可自行查询。

datetime模块

datetime模块主要定义了以下几个类:

datetime.date                         表示日期的类

datetime.time                         表示时间的类

datetime.datetime                  表示日期时间的类

datetime.timedelta                 表示时间间隔

其中 datetime.timedelta(days)  接受一个参数,默认为  天,也可以指定 datetime.timedelta(hours=4),可直接跟datetime的其它类进行时间运算。

time 与 datetime 模块的常用方法的更多相关文章

  1. Python第十五天 datetime模块 time模块 thread模块 threading模块 Queue队列模块 multiprocessing模块 paramiko模块 fabric模块

    Python第十五天  datetime模块 time模块   thread模块  threading模块  Queue队列模块  multiprocessing模块  paramiko模块  fab ...

  2. python的datetime模块处理时间

    python的datetime模块主要用来处理时间,里面包含很多类,包括timedelay,date,time,datetime等 开发中经常会用到模块里面的datetime类,这是一个表示日期时间的 ...

  3. Python3学习之路~5.2 time & datetime模块

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

  4. collections、time和datetime模块

    主要内容: 一.collections模块 二.time模块 三.datetime模块 1️⃣  collection模块 1.什么是collections模块.干什么用? collections模块 ...

  5. time&datetime模块

    在Python中,和时间处理相关的模块有time,datatime,calendar(不常用)三个. UTCC(Coordinated Universal Time,世界协调时)亦即格林威治天文时间, ...

  6. python中time、datetime模块的使用

    目录 python中time.datetime模块的使用 1.前言 2.time模块 1.时间格式转换图 2.常用方法 3.datetime模块 python中time.datetime模块的使用 1 ...

  7. python中datetime模块

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

  8. python datetime模块参数详解

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

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

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

随机推荐

  1. django 后端反向生成url

    导入模块 from django.core.urlresolvers import reverse reverse() reverse(viewname, urlconf=None, args=Non ...

  2. Unity 让物体朝摄像机观察方向移动,已摇杆方向转向

    using System.Collections;using System.Collections.Generic;using UnityEngine; [RequireComponent(typeo ...

  3. spring boot常见问题

    1.什么是springboot 用来简化spring应用的初始搭建以及开发过程 使用特定的方式来进行配置(properties或yml文件) 创建独立的spring引用程序 main方法运行 嵌入的T ...

  4. Qt文档阅读笔记-QGraphicsItem::paint中QStyleOptionGraphicsItem *option的进一步认识

    官方解析 painter : 此参数用于绘图;option : 提供了item的风格,比如item的状态,曝光度以及详细的信息:widget : 想画到哪个widget上,如果要画在缓存区上,这个参数 ...

  5. 完整的多文件上传实例(java版)

    昨天刚刚做了一个文件列表上传,后端很简单,用 MultipartFile[] files 获取文件流数组,后端就当IO流操作就可以,似乎好像没啥好写的,但是!!!!!前端是真的糙单.要是自己写一个前端 ...

  6. HDFS配置参数及优化之实战经验(Linux hdfs)

    HDFS优化之实战经验 Linux系统优化 一.禁止文件系统记录时间 Linux文件系统会记录文件创建.修改和访问操作的时间信息,这在读写操作频繁的应用中将带来不小的性能损失.在挂载文件系统时设置no ...

  7. win10配置java开发环境

    安装java(JDK) Oracle官网下载地址 选择JavaSE > Downloads 选择最新版本 > Download Accept License Agreement 选择要下载 ...

  8. Deep Learning--week1~week3

    week1 一张图片,设像素为64*64, 颜色通道为红蓝绿三通道,则对应3个64*64实数矩阵 为了用向量表示这些矩阵,将这些矩阵的像素值展开为一个向量x作为算法的输入 从红色到绿色再到蓝色,依次按 ...

  9. 论文笔记:Learning Attribute-Specific Representations for Visual Tracking

    Learning Attribute-Specific Representations for Visual Tracking AAAI-2019 Paper:http://faculty.ucmer ...

  10. 何给域名配置https证书

    原文链接:https://www.cnblogs.com/ymwang/p/6893105.html http和https的区别就是,后者在网络传输过程中会很安全,原因就是给http安装了SSL证书. ...