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模块 ...
随机推荐
- 搬运2:早期写的探究printf
目录: 1. 关于printf格式化输出 2. printf的一般形式 3. 转换说明 4. 格式化输出的意义 5. 转换说明修饰符 6. 修饰符中的标记 7. printf的返回值 ps:共3250 ...
- c语言编程基础入门必备知识
数据类型 基本数据类型 类型名称说明char字符类型存放字符的ASCII码int整型存放有符号整数short短整型存放有符号整数long长整型存放有符号整数long long存放有符号整数float单 ...
- 广域网(ppp协议、HDLC协议)
文章转自:https://blog.csdn.net/weixin_43914604/article/details/105028759 学习课程:<2019王道考研计算机网络> 学习目的 ...
- 常用JAVA API :HashSet 和 TreeSet
set容器的特点是不包含重复元素,也就是说自动去重. HashSet HashSet基于哈希表实现,无序. add(E e)//如果容器中不包含此元素,则添加. clear()//清空 contain ...
- 把数组排成最小的数 牛客网 剑指Offer
把数组排成最小的数 牛客网 剑指Offer 题目描述 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能 ...
- 分布式事务(四)之TCC
在电商领域等互联网场景下,传统的事务在数据库性能和处理能力上都暴露出了瓶颈.在分布式领域基于CAP理论以及BASE理论,有人就提出了柔性事务的概念.在业内,关于柔性事务,最主要的有以下四种类型:两阶段 ...
- C++ IO基础
一:c++I/O处理,按照数据输入输出的过程,形象的将其看做流.数据在流中进行传播. 所有的流有两个基类:ios和streambuf类 streambuf:提供对缓冲区的基本操作,设置缓冲区等 ios ...
- istio基础详解
1.Istio介绍? 官方文档:https://istio.io/docs/concepts/what-is-istio/ 中文官方文档:https://istio.io/zh/docs/concep ...
- Mysql教程:(一)数据库常用基础命令
数据库常用命令 1.登录 进入数据库,在win系统下,打开cmd,切换用户权限,进入root: 沒权限,用root登录: mysql -uroot 如果root有密码: mysql -uroot -p ...
- PAT甲级1074 Reversing Linked List (25分)
[程序思路] 先根据地址按顺序读入节点,入栈,当栈里的元素个数等于k时全部出栈,并按出栈顺序保存,最后若栈不为空,则全部出栈并按出栈的稀饭顺序保存,最后输出各节点 注意:输入的节点中有可能存在无用节点 ...