Python time&datetime模块
1.time&datetime模块
time&datetime是时间模块,常用以处理时间相关问题
time.time() #返回当前时间的时间戳timestamp time.sleep() #睡眠时间,默认秒 time.gmtime() #时间戳转换成UTC时间的struct_time time.localtime() #时间戳转换成返回本地时间的struct_time time.asctime() #struct_time转换成本地时间的标准化string时间 time.ctime() #时间戳转换成本地时间的标准化string时间 time.mktime() #struct_time转换成本地时间的时间戳timestamp time.strftime() #解析struct_time,自定义格式化显示时间 time.strptime() #解析格式化时间,返回struct_time
常用功能使用说明
# -*- coding:utf-8 -*-
# Author:Wong Du '''
time&datetime是时间模块,
常用以处理时间相关问题
''' import time
'''
time模块时间的3种形式
1.timestamp 时间戳
e.g. 1521164794.9068174
2.struct_time 结构化时间(元组形式表示)
e.g. time.struct_time(tm_year=2078, tm_mon=6, tm_mday=25, tm_hour=22,
tm_min=57, tm_sec=22, tm_wday=5, tm_yday=176, tm_isdst=0)
3.string_time 字符串表示(时间的人性化显示,一般人类可读)
e.g. Fri Mar 16 09:49:13 2018
'''
# 时间戳
print(time.time())
# 结构化
print(time.localtime())
# 字符串
print(time.asctime()) # time.asctime(p_tuple=None) --> string,
# 解析一个tuple结构化时间,返回其标准化时间的string,如:Fri Mar 16 09:51:26 2018,
# 若tuple为空,则返回当前时间的string
m = (2018, 3, 16, 10, 0, 52, 4, 75, 0)
print(time.asctime(m)) # time.ctime(seconds=None) --> string,
# 时间戳转化成标准化时间的string,若seconds为空,
# 则返回当前时间的string
print(time.ctime(2222222222)) # time.gmtime(seconds=None) --> struct_time,
# 时间戳转换成UTC时间的struct_time,和中国时间差8个小时
# 若seconds为空,则返回当前UTC时间的struct_time
print(time.gmtime(2222222)) # time.localtime(seconds=None) --> struct_time,
# 时间戳转换成struct_time,
# 若seconds为空,则返回当前的struct_time
print(time.localtime(2222222)) # time.mktime(p_tuple) --> timestamp
# 把struct_time转换成时间戳timestamp,
print(time.mktime(m)) # 获取本地当前时间的时间戳
print(time.time()) # 设置睡眠时间,单位秒
time.sleep(0.1) # time.strftime(format,p_tuple=None) --> format_time
# 解析p_tuple,自定义格式显示,如:2018-01-03,
# 若p_tuple为空,则格式化显示本地当前时间
print(time.strftime("%Y-%m-%d %H:%M:%S",m)) # time.strptime(string,format) --> struct_time,
# 解析string,获取到string表示的struct_time
print(time.strptime("2018-1-1","%Y-%m-%d")) import datetime
# 获取本地当前时间,格式: %Y-%m-%d %H:%M:%S
print(datetime.datetime.now()) # 时间加减,默认顺序:days,秒,微秒,毫秒,minutes,hours,weeks
print(datetime.datetime.now() + datetime.timedelta(1)) #+1day
print(datetime.datetime.now() + datetime.timedelta(-3)) #-3day
print(datetime.datetime.now() + datetime.timedelta(minutes=-10)) #-10minutes
print(datetime.datetime.now() + datetime.timedelta(weeks=1)) #+7day # 时间替换
"""
replace(self, year=None, month=None, day=None, hour=None,
minute=None, second=None, microsecond=None, tzinfo=True)
"""
print(datetime.datetime.now().replace(year=2022))
常用功能详解
其他
时间格式化显示: %Y Year with century as a decimal number.
%m Month as a decimal number [01,12].
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%M Minute as a decimal number [00,59].
%S Second as a decimal number [00,61].
%z Time zone offset from UTC.
%a Locale's abbreviated weekday name.
%A Locale's full weekday name.
%b Locale's abbreviated month name.
%B Locale's full month name.
%c Locale's appropriate date and time representation.
%I Hour (12-hour clock) as a decimal number [01,12].
%p Locale's equivalent of either AM or PM.
三种时间格式关系示例图

Python time&datetime模块的更多相关文章
- python的datetime模块处理时间
python的datetime模块主要用来处理时间,里面包含很多类,包括timedelay,date,time,datetime等 开发中经常会用到模块里面的datetime类,这是一个表示日期时间的 ...
- 基于Python的datetime模块和time模块源码阅读分析
目录 1 前言 2 datetime.pyi源码分步解析 2.1 头部定义源码分析 2.2 tzinfo类源码分析 2.3 date类源码分析 2.4 time类源码分析 2.5 timedelta ...
- 孤荷凌寒自学python第二十七天python的datetime模块及初识datetime.date模块
孤荷凌寒自学python第二十七天python的datetime模块及初识datetime.date模块 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.datetime模块 dateti ...
- python使用datetime模块计算各种时间间隔的方法
python使用datetime模块计算各种时间间隔的方法 本文实例讲述了python使用datetime模块计算各种时间间隔的方法.分享给大家供大家参考.具体分析如下: python中通过datet ...
- python中datetime模块
Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供 的接口与C标准库time.h基本一致.相比于time模块 ...
- python处理时间--- datetime模块
1 Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供的接口与C标准库time.h基本一致.相比于tim ...
- Python,datetime模块实例
Python的标准模块datetime模块,在我们的工作中应用非常频繁,下面对datetime中常用的方法进行了总结和测试:对每一个方法都使用了单元测试框架Unittest来配合测试. 主要的类型有: ...
- Python的datetime模块分析
datetime模块用于是date和time模块的合集,datetime有两个常量,MAXYEAR和MINYEAR,分别是9999和1. datetime模块定义了5个类,分别是 1.datetime ...
- python——从datetime模块探索python的数据架构
问题出现于我试图向自建网页中加入实时时间开始. 我之前已经知道python中有有关事件和日期的模块datetime.以下导入datetime并作实验. >>> import date ...
- python中 datetime模块的详解(转载)
Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供 的接口与C标准库time.h基本一致.相比于time模块 ...
随机推荐
- RabbitMQ的一些理解和笔记
在这篇博客中,简单记录一下 rabbitmq 服务器中一些基本的概念. Connection: connection 为 TCP连接,是我们的应用程序和RabbitMQ服务器真正发送和接收数据的地方. ...
- GT考试
比较神仙的$dp+KMP+Matrix$综合题目,比较值得一写 $0x00$:首先我打了一个爆搜 不过对正解并无任何启发...(逗比发言请忽略) $0x01$:基础$dp$ 状态还是比较好设的, 考虑 ...
- 上午小测3 T1 括号序列 && luogu P5658 [CSP/S 2019 D1T2] 括号树 题解
前 言: 一直很想写这道括号树..毕竟是在去年折磨了我4个小时的题.... 上午小测3 T1 括号序列 前言: 原来这题是个dp啊...这几天出了好几道dp,我都没看出来,我竟然折磨菜. 考试的时候先 ...
- 8M的摄像头,30fps摄像时,60hz的LCD刷新频率,请问camera每秒向BB传输多少数据,如何计算
8M的摄像头,30fps摄像时,60hz的LCD刷新频率,请问camera每秒向BB传输多少数据,如何计算 xiang2012 Post at 2012/8/7 10:37:33 8M的摄像头,30f ...
- ASP.NET Core 学习笔记 第四篇 ASP.NET Core 中的配置
前言 说道配置文件,基本大多数软件为了扩展性.灵活性都会涉及到配置文件,比如之前常见的app.config和web.config.然后再说.NET Core,很多都发生了变化.总体的来说技术在进步,新 ...
- nodejs 连接 mysql 查询事务处理
自己用 mysql 很多次的,然后又是主玩nodejs的.专门写一篇文章来说说nodejs连接mysql数据库.在使用之前,请检查计算机是否具有一下环境! nodejs 执行环境. mysql数据库环 ...
- vscode插件集合整理
针对PEPE8进行代码规范提示,安装flake8之后写代码的时候编辑器就会提示哪里出错,代码格式不规范也会提示,具体安装方式如下: 1.pip install flake8 2.安装flake8成功后 ...
- triangle leetcode C++
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- poj 3417 Network (LCA,路径上有值)
题意: N个点,构成一棵树.给出这棵树的结构. M条边,(a1,b1)...(am,bm),代表给树的这些点对连上边.这样就形成了有很多环的一个新"树". 现在要求你在原树中断一条 ...
- 『学了就忘』Linux基础命令 — 18、Linux命令的基本格式
目录 1.命令提示符说明 2.命令的基本格式 (1)举例ls命令 (2)说明ls -l命令的 输出内容 1.命令提示符说明 [root@localhost ~] # []:这是提示符的分隔符号,没有特 ...