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模块 ...
随机推荐
- OO_JAVA_JML系列作业_单元总结
OO_JAVA_JML系列作业_单元总结 (1)梳理JML语言的理论基础.应用工具链情况 简单梳理 以下三者是jml规格里的核心,对一个方法功能和属性的限制: requires子句:规定方法的前置条件 ...
- OO_JAVA_表达式求导
OO_JAVA_表达式求导_第一弹 ---------------------------------------------------表达式提取部分 词法分析 首先,每一个表达式内部都存在不可 ...
- 使用Mybatis的TypeHandler加解密数据
使用Mybatis的TypeHandler加解密数据 一.背景 二.解决方案 三.需求 四.实现思路 1.编写一个实体类,凡是此实体类的数据都表示需要加解密的 2.编写一个加解密的`TypeHandl ...
- DDD领域驱动设计-案例建模设计-Ⅲ
1. 背景 参考<DDD领域驱动设计-案例需求文档>,本文将构建实体,聚合根详述领域驱动中的建模设计.构建实体,聚合根的一些原则或方法,将在后续文章中说明. 2. 建模设计 2.1. 实体 ...
- C++ Boost signal2信号/插槽
#include "stdafx.h" #include "boost/signals2.hpp" #include "boost/bind.hpp& ...
- Shadertoy 教程 Part 3 - 矩形和旋转
Note: This series blog was translated from Nathan Vaughn's Shaders Language Tutorial and has been au ...
- linux下创建文件的文件权限问题
今天发现创建文件的权限和自己规定的权限不一致,了解到了权限掩码的问题,这里总结一下. 首先权限掩码umask是chmod配套的,总共为4位(gid/uid,属主,组权,其它用户的权限),不过通常我们都 ...
- 两个栈实现队列 牛客网 程序员面试金典 C++ Python
两个栈实现队列 牛客网 程序员面试金典 C++ Python 题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. C++ //run:5ms memeory ...
- 对于multitaper多窗口谱估计的理解及步骤 (对应matlab中pmtm函数)谱减法相关
对于多窗口谱估计的理解 目录 对于多窗口谱估计的理解 0. 缘起 1. PMTM 含义 2. 与我们常用的周期谱估计的区别 3. 计算过程 5. 多窗/单窗谱估计结果对比 6. 程序如何生成多窗 - ...
- Java测试开发--Comparable和Comparator接口(五)
Comparable 简介Comparable 是排序接口.若一个类实现了Comparable接口,就意味着"该类支持排序".此外,"实现Comparable接口的类的对 ...