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模块的更多相关文章

  1. python的datetime模块处理时间

    python的datetime模块主要用来处理时间,里面包含很多类,包括timedelay,date,time,datetime等 开发中经常会用到模块里面的datetime类,这是一个表示日期时间的 ...

  2. 基于Python的datetime模块和time模块源码阅读分析

    目录 1 前言  2 datetime.pyi源码分步解析 2.1 头部定义源码分析 2.2 tzinfo类源码分析 2.3 date类源码分析 2.4 time类源码分析 2.5 timedelta ...

  3. 孤荷凌寒自学python第二十七天python的datetime模块及初识datetime.date模块

    孤荷凌寒自学python第二十七天python的datetime模块及初识datetime.date模块 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.datetime模块 dateti ...

  4. python使用datetime模块计算各种时间间隔的方法

    python使用datetime模块计算各种时间间隔的方法 本文实例讲述了python使用datetime模块计算各种时间间隔的方法.分享给大家供大家参考.具体分析如下: python中通过datet ...

  5. python中datetime模块

    Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供 的接口与C标准库time.h基本一致.相比于time模块 ...

  6. python处理时间--- datetime模块

    1   Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供的接口与C标准库time.h基本一致.相比于tim ...

  7. Python,datetime模块实例

    Python的标准模块datetime模块,在我们的工作中应用非常频繁,下面对datetime中常用的方法进行了总结和测试:对每一个方法都使用了单元测试框架Unittest来配合测试. 主要的类型有: ...

  8. Python的datetime模块分析

    datetime模块用于是date和time模块的合集,datetime有两个常量,MAXYEAR和MINYEAR,分别是9999和1. datetime模块定义了5个类,分别是 1.datetime ...

  9. python——从datetime模块探索python的数据架构

    问题出现于我试图向自建网页中加入实时时间开始. 我之前已经知道python中有有关事件和日期的模块datetime.以下导入datetime并作实验. >>> import date ...

  10. python中 datetime模块的详解(转载)

    Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供 的接口与C标准库time.h基本一致.相比于time模块 ...

随机推荐

  1. BUAA 软件工程个人作业

    BUAA 软件工程 个人项目作业 Author: 17373015 乔玺华 教学班级 :005 项目地址:https://github.com/JordenQiao/SE_Homework_Perso ...

  2. SpringBoot加密配置属性

    一.背景 在系统中的运行过程中,存在很多的配置属性,比如: 数据库配置.阿里云配置 等等,这些配置有些属性是比较敏感的,是不应直接以明文的方式出现在配置文件中,因此对于这些配置我们就需要加密来处理. ...

  3. 热身训练1 ping ping ping

    点此进入 题意: 一棵树,n+1 个节点,以0号节点为根,给出端点(a,b),节点a到节点b的路径上,至少有一个点是"坏掉的",求"坏掉的点"最少 分析: St ...

  4. 2021.8.3考试总结[NOIP模拟29]

    T1 最长不下降子序列 数据范围$1e18$很不妙,但模数$d$只有$150$,考虑从这里突破. 计算的式子是个二次函数,结果只与上一个值有关,而模$d$情况下值最多只有$150$个,就证明序列会出现 ...

  5. vim 常用操作技巧

    记录常用的vim操作技巧,基本满足90%的日常编辑使用. 文档操作 vim test.txt 打开当前目录下的test.txt文档,若不存在则创建该文件 :w 保存当前修改到文件 :w bak.txt ...

  6. 0x02

    #include<bits/stdc++.h> using namespace std; int n,a[10][10],vis[10],ans,b[10][10]; inline int ...

  7. Luogu P1525 [NOIp2010提高组]关押罪犯 | 并查集

    题目链接 这一道题,我用了并查集来做.在此题中,并查集的作用就是:将同一个监狱里的罪犯合并到一起. 思路:将每对罪犯之间的怨气值从大到小排序,再依次把他们分到不同的两个监狱里,当发现这一对罪犯已经在同 ...

  8. hdu 2058 The sum problem(简单因式分解,,)

    Problem Description Given a sequence 1,2,3,......N, your job is to calculate all the possible sub-se ...

  9. RabbitMQ(六)消息幂等性处理

    一.springboot整合rabbitmq 我们需要新建两个工程,一个作为生产者,另一个作为消费者.在pom.xml中添加amqp依赖: <dependency> <groupId ...

  10. java动态编译——tools.jar问题

    笔者在学习中写了一段简单的动态编译代码,但编译一直无法通过,起初认为受路径中存在汉字影响,修改路径后仍然没有解决.最终定位错误是:Java在进行动态编译的时候需要用到tools.jar资源包,若too ...