时间格式

一个名词:

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. JAVA课堂测试之一位数组可视化

    代码: package test;//求最大子数组 import java.util.Scanner; import javax.swing.JOptionPane; public class shu ...

  2. linux下wrk的安装

    wrk是linux下开源的性能测试工具,并且只能在linux下运行,下面介绍下安装教程(以ubantu18.04环境为例): 1.预先安装git,如:apt install git 2.从git上拉取 ...

  3. appium+python3+pycharm踩得坑

    错误: selenium.common.exceptions.WebDriverException: Message: A new session could not be created. (Ori ...

  4. 【题解】Luogu P4436 [HNOI/AHOI2018]游戏

    原题传送门 \(n^2\)过百万在HNOI/AHOI2018中真的成功了qwqwq 先将没门分格的地方连起来,枚举每一个块,看向左向右最多能走多远,最坏复杂度\(O(n^2)\),但出题人竟然没卡(建 ...

  5. java导出excel,多表头合并

    要求结果图如下: 有空补充具体逻辑 参考:https://blog.csdn.net/dj0721/article/details/72463042 HSSFColor  背景颜色选择  参考:htt ...

  6. day08文件操作的三步骤,基础的读,基础的写,with...open语法,文件的操作模式,文件的操作编码问题,文件的复制,游标操作

    复习 ''' 类型转换 1.数字类型:int() | bool() | float() 2.str与int:int('10') | int('-10') | int('0') | float('-.5 ...

  7. Windows 控制面板调用命令

    使用命令打开 Windows 控制面板指定页面 control.exe /name microsoft.folderoptions 启动资源管理器的 文件夹属性 选项卡 control.exe /na ...

  8. appium-android 遇到swipe函数无法使用的问题及解决办法

    问题:cannot resolve method swipe() 问题出现原因:File->Project Structure->Modules->Dependencies-> ...

  9. 优雅的使用windows

    1. 快捷键 win+数字键:打开任务栏对应位置的程序 eg:快捷打开程序,重复该组合键有两个效果,如果只打开了一个窗口,再次按下组合键即可最小化,如果打开了多个窗口,则可以在同一程序的多个窗口中切换 ...

  10. 【Git】Git使用记录: 基于git ignore文件将remote上的文件untrack

    话不多说直接上步骤: git bash直接干到你的code. 直接敲命令: git rm -r --cached . rm是remove 命令 -r将允许递归删除 -cached只会从索引中删除文件. ...