美文网首页
python之time模块3

python之time模块3

作者: 闲云野鹤_23dd | 来源:发表于2021-03-12 18:44 被阅读0次

python之time模块3

时间计算

datetime模块 提供了一些时间计算相关的函数,例如 前几天,后几天,前几周,后几周等,还可以计算两个时间之间的差值

前几天/周

import datetime

now = datetime.datetime.now()
# 三天前
three_days_ago = now + datetime.timedelta(days=-3)
# 三周前
three_weeks_ago = now + datetime.timedelta(weeks=-3)

print(now)
print(three_days_ago)
print(three_weeks_ago)

后几天/周

now = datetime.datetime.now()

# 三天后
three_days_later = now + datetime.timedelta(days=3)

# 三周后
three_weeks_later = now + datetime.timedelta(weeks=3)

print(now)
print(three_days_later)
print(three_weeks_later)

计算时间差

使用 datetime.datetime.strptime() 转换字符串时间 为 datetime 对象,然后进行相减运算,即可获得 时间差

dd = '2019-03-17 11:00:00'
dd = datetime.datetime.strptime(dd, "%Y-%m-%d %H:%M:%S")

ff = '2019-09-17 10:00:00'
ff = datetime.datetime.strptime(ff, "%Y-%m-%d %H:%M:%S")

dv =  ff-dd
# 相差天数
print(dv.days)
# 相差总秒数
print(dv.total_seconds())

## 时分秒单位相差的秒数
print(dv.seconds)
## 毫秒单位相差的秒数
print(dv.microseconds)

相关文章

  • Python常用模块

    Python常用模块之time模块 Python常用模块之os模块 Python常用模块之sys模块 Python...

  • Python 入门之 内置模块 - time模块

    Python 入门之 内置模块 -- time模块 1、time模块 ​ time翻译过来就是时间,这个模块是与时...

  • Python3中datetime模块常用功能总结

    Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime。time模块我在之...

  • python的datetime

    Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime。time模块我在之...

  • python之time模块3

    python之time模块3 时间计算 datetime模块 提供了一些时间计算相关的函数,例如 前几天,后几天,...

  • Python 时间与日期处理

    [TOC] 内置库 time 模块 https://docs.python.org/3/library/time....

  • Python之time模块

    前言 time模块是Python3的内置模块,常用方法如下 实例 输出

  • Python正式课第十四天

    Python常用模块 一、时间处理模块 1. time.time() time time() 返回当前时间的时间戳...

  • time

    python模块之time time:官方文档是最好的模块表达说明。 通常处理日期和时间的方式在于时间戳和字符串式...

  • python之time模块

网友评论

      本文标题:python之time模块3

      本文链接:https://www.haomeiwen.com/subject/tkbdzktx.html