python常用的处理时间的库有:datetime,time,calendar。datetime库包括了date(储存日期:(年、月、日),time(储存时间:(小时、分、秒和微秒),timedelta代表两个datetime之间的差(天、秒、微秒)。

一、datetime处理日期

  • datetime.now():当前时间
  • datetime.timedelta():计算相差天数和秒数
  • datetime():创建datetime格式

1. datetime.now()

from datetime import datetime
now=datetime.now()
print(f'当前时间:{now}')
print(f'{now.year}年{now.month}月{now.day}日')

2. datetime.timedelta()

timedelta计算相差天数和秒数

from datetime import timedelta
delta = timedelta(days=1,seconds=1300)
delta_seconds = delta.total_seconds()
print(delta_seconds/3600)
print(delta_seconds)

from datetime import timedelta
start=datetime(2019,4,1)
#计算50天后是哪一天
start+timedelta(50)

3. datetime( )

(1)创建datetime格式

datetime(%Y,%m,%d,%H,%M,%S)

datetime共有6个参数,分别代表的是年月日时分秒。其中年月日是必须要传入的参数,时分秒可以不传入,默认全为零。

二、时间格式处理及转换

1. str.strftime('格式')

(1)datetime格式转换成字符串

stamp = datetime(2019,4,6)

  datetime.strptime只能根据设定的时间格式来处理指定的字符串,如果列表里(list)包含不止一种格式的字符串,如datestr=['12/20/2018','12/11/2018','2018-10-18'],使用datetime.strptime就很难处理了。这时候,可以使用第三方dateutil包的parser.parse方法。这个包能够解析大部分人类可理解的日期表示。

(2)parser.parse

日期出现在月份之前,可以通过传递dayfirst=True来表明

from dateutil.parser import parse
datestr=['12/20/2018','20180210','2019-01-10']
#转换成datetime格式
new_d=[parse(d) for d in datestr]
#统一为12/20/2018格式
d1=[d.strftime('%m/%d/%Y') for d in new_d]
d2=[d.strftime('%Y%m%d') for d in new_d]
d3=[d.strftime('%Y-%m-%d') for d in new_d]
d4=[d.strftime('%y-%m-%d') for d in new_d]
print(f'datetime格式:\n{new_d}')
print(f'"月/日/年"格式:\n {d1}')
print(f'"年月日"格式:\n{d2}')
print(f'"年-月-日格式":\n{d3}')
print(f'"年(后两位)-月-日"格式:\n{d4}')

datetime模块处理时间的更多相关文章

  1. python的datetime模块处理时间

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

  2. Python处理时间 time && datetime 模块

    Python处理时间 time  &&  datetime 模块 个人整理,获取时间方式: import datetime import time #获取当前时间:Thu Nov 03 ...

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

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

  4. python中datetime模块

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

  5. python datetime模块参数详解

    Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块,它提供 的接口与C标准库time.h基本一致.相比于time模块,datetime模块的接 ...

  6. python time模块和datetime模块详解

    一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共 ...

  7. python-Day5-深入正则表达式--冒泡排序-时间复杂度 --常用模块学习:自定义模块--random模块:随机验证码--time & datetime模块

    正则表达式   语法:             mport re #导入模块名 p = re.compile("^[0-9]") #生成要匹配的正则对象 , ^代表从开头匹配,[0 ...

  8. python datetime模块strptime/strptime format常见格式命令_施罗德_新浪博客

    python datetime模块strptime/strptime format常见格式命令_施罗德_新浪博客     python datetime模块strptime/strptime form ...

  9. Python datetime模块的datetime类

    datetime模块定义了下面这几个类: datetime.date:表示日期的类.常用的属性有year, month, day. datetime.time:表示时间的类.常用的属性有hour, m ...

随机推荐

  1. module.exports与exports

    API文档是枯燥的,下面本人收集了一些论坛经常有人疑问和开源代码中经常遇到的案例供大家研究一下. module.exports与exports的区别 每一个node.js执行文件,都自动创建一个mod ...

  2. scrapy_redis 相关: 查看保存的数据

    0.参考资料 https://redis.io/topics/data-types-intro An introduction to Redis data types and abstractions ...

  3. 洛谷P4482 [BJWC2018]Border 的四种求法 字符串,SAM,线段树合并,线段树,树链剖分,DSU on Tree

    原文链接https://www.cnblogs.com/zhouzhendong/p/LuoguP4482.html 题意 给定一个字符串 S,有 q 次询问,每次给定两个数 L,R ,求 S[L.. ...

  4. UOJ#218. 【UNR #1】火车管理 线段树 主席树

    原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ218.html 题解 如果我们可以知道每次弹出栈之后新的栈顶是什么,那么我们就可以在一棵区间覆盖.区间求和 ...

  5. Rank of Tetris 拓扑排序+并查集

    Problem Description 自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子: ...

  6. 在'for'循环中获取索引

    ints = [8, 23, 45, 12, 78] 当使用循环遍历它时,在这种情况下如何访问循环索引,从1到5? 最普遍的办法是设置索引变量(通常在C或PHP等语言中使用),但这被认为是非pytho ...

  7. Classy Numbers

    http://codeforces.com/group/w1oiqifZbS/contest/1036/problem/C ①先查找,存入vector(dfs)-->排序(sort)--> ...

  8. KVM嵌套虚拟化nested之CPU透传

    嵌套式虚拟nested是一个可通过内核参数来启用的功能.它能够使一台虚拟机具有物理机CPU特性,支持vmx或者svm(AMD)硬件虚拟化.该特性需要内核升级到Linux 3.X版本 ,所以在cento ...

  9. C#订阅与发布标准实现

    大概看了下C#官方提供的IObservable接口以及IObserver接口来实现发布和订阅,写的很标准,很有代表性,做下笔记,以后要是项目需要用到发布订阅再基于自己的需求改: public clas ...

  10. 为什么很多应用都安装在/usr/local目录下?

    首先,Linux的官方文档FHS对该目录的说明:http://www.linuxbase.org/betaspecs/fhs/fhs/ch04s09.html The /usr/local hiera ...