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模块 ...
随机推荐
- dice_game攻防世界进阶区
dice_game XCTF 4th-QCTF-2018 前言,不得不说,虽然是个简单题但是还是要记录一下,来让自己记住这些东西. 考察的知识点是: 1.cdll_loadlibrary加载对应库使得 ...
- 【做题记录】CF1444A Division
CF1444A Division 题意: 给定 \(t\) 组询问,每组给两个数 \(p_i\) 和 \(q_i\) ,找出最大的整数 \(x_i\) ,要求 \(p_i\) 可被 \(x_i\) 整 ...
- js fetch异步请求使用详解
目录 认识异步 fetch(url) response.json() 结合async和await 异常处理 post请求 认识异步 首先我们得明白请求是一个异步的过程. 因为请求需要时间向服务器发送请 ...
- max-points-on-a-line leetcode C++
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- hdu 5101 Select (二分+单调)
题意: 多多有一个智商值K. 有n个班级,第i个班级有mi个人.智商分别是v1,v2,.....vm. 多多要从这些人中选出两人.要求两人智商和大于K,并且两人不同班.问总共有多少种方案. 数据范围: ...
- SSH 信任关系建立
需求hostA通过ssh登陆到hostB,实现免密登陆,以及SCP的免密传送文件 由于hostA要登陆到hostB 首先需要在hostA上生成密钥,使用以下命令 ssh-keygen -t rsa 按 ...
- 『学了就忘』Linux基础命令 — 25、文件基本权限的管理
目录 1.文件和目录的默认权限 2.umask默认权限 (1)查看系统的umask权限 (2)用八进制数值显示umask权限 (3)umask权限的计算方法 (4)注意:umask默认权限的计算绝不是 ...
- Go语言实现APPID登录
package thirdparty import ( "crypto/rsa" "fmt" "github.com/dgrijalva/jwt-go ...
- 【AI测试】人工智能 (AI) 测试--第二篇
测试用例 人工智能 (AI) 测试 或者说是 算法测试,主要做的有三件事. 收集测试数据 思考需要什么样的测试数据,测试数据的标注 跑测试数据 编写测试脚本批量运行 查看数据结果 统计正确和错误的个数 ...
- c++学习笔记2(const关键词的用法)
定义常量指针 优势(便于类型检查,define无类型检查(目前不是很理解)) (函数参数为常量指针时,可避免函数内部不小心改变参数指针所指的地方,如有出现此类语句,编译则会报错) strcpy:复制字 ...