脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|shell|

服务器之家 - 脚本之家 - Python - 一文带你吃透Python中的日期时间模块

一文带你吃透Python中的日期时间模块

2023-03-02 11:57Charge8 Python

Python 提供了 日期和时间模块用来处理日期和时间,还可以用于格式化日期和时间等常见功能。这篇文章就来带大家了解一下它的使用,需要的可以参考一下

Python 提供了 日期和时间模块用来处理日期和时间,还可以用于格式化日期和时间等常见功能。

  • 时间间隔是以秒为单位的浮点小数。
  • 每个时间戳都以自从 1970 年 1 月 1 日午夜(历元)经过了多长时间来表示。

一、time模块使用

Time 模块包含了大量内置函数,既有时间处理的,也有转换时间格式的。

1、获取当前时间

?
1
2
3
4
5
6
7
8
9
10
11
import time
 
# 获取时间戳 返回的是浮点型
# 作用 用来计算时间差
print(time.time()) # 1676775733.1080997
 
# 获取当地时间   返回的是结构化时间
print(time.localtime())
 
#  获取UTC时间 返回的还是结构化时间  比中国时间少8小时
print(time.gmtime())

一文带你吃透Python中的日期时间模块

1.1 时间元组

时间元组:Python函数用一个元组装起来的9组数字处理时间。

一文带你吃透Python中的日期时间模块

上面也就是 struct_time 元组。这种结构具有如下属性:

一文带你吃透Python中的日期时间模块

2、格式化时间

2.1 获取格式化的时间

?
1
2
3
4
5
6
7
import time
 
# 获取本地时间
localtime = time.asctime(time.localtime(time.time()))
#localtime = time.asctime(time.localtime())
print("本地时间为 :", localtime)
## 本地时间为 : Mon Feb 20 20:49:10 2023

2.2 格式化时间转换

使用 time 模块的 strftime 方法来格式化日期,通过 strptime方法来格式化字符串。

?
1
2
3
4
5
6
7
8
9
10
# 时间转换为格式字符串
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())))
 
# 格式字符串转换为时间
str = "2023-02-14 05:20:00"
print(time.strptime(str, "%Y-%m-%d %H:%M:%S"))
 
# 格式字符串转换为时间戳
print(time.mktime(time.strptime(str, "%Y-%m-%d %H:%M:%S")))

一文带你吃透Python中的日期时间模块

Python中时间日期格式化符号:

  • %y 两位数的年份表示(00-99)
  • %Y 四位数的年份表示(000-9999)
  • %m 月份(01-12)
  • %d 月内中的一天(0-31)
  • %H 24小时制小时数(0-23)
  • %I 12小时制小时数(01-12)
  • %M 分钟数(00=59)
  • %S 秒(00-59)
  • %a 本地简化星期名称
  • %A 本地完整星期名称
  • %b 本地简化的月份名称
  • %B 本地完整的月份名称
  • %c 本地相应的日期表示和时间表示
  • %j 年内的一天(001-366)
  • %p 本地A.M.或P.M.的等价符
  • %U 一年中的星期数(00-53)星期天为星期的开始
  • %w 星期(0-6),星期天为星期的开始
  • %W 一年中的星期数(00-53)星期一为星期的开始
  • %x 本地相应的日期表示
  • %X 本地相应的时间表示
  • %Z 当前时区的名称
  • %% %号本身

二、datetime模块

datetime模块提供了对时间和日期进行操作的功能。

datatime模块通过date类、time类、datetime类、timedelta类、timezone类等来获取或者操作时间、日期。

与time模块相比,datetime模块提供的接口更直观、易用,功能也更加强大。

1、获取当前时间

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from datetime import datetime
 
# 获取时间 获取当前时间 并且返回的是格式化字符时间
now = datetime.now()
print(now)
print(type(now))
print(now.year)
print(now.month)
print(now.day)
print(now.hour)
print(now.minute)
print(now.second)
print(now.microsecond)
 
# 手动指定时间
print(datetime(2023, 2, 20, 20, 50, 00))
 
# 替换某个时间单位的值
print(now.replace(year=2024))
 
d1 = datetime.now()
d2 = datetime(2024, 2, 20, 20, 50, 00)
print(d1 - d2) 

一文带你吃透Python中的日期时间模块

2、格式化时间

(1)datetime 转 字符串,通过 strftime()函数。

?
1
2
3
4
5
6
7
8
9
10
11
from datetime import datetime
 
now = datetime.now()
# 强制转换字符串
print(str(now))
 
# 格式化日期 转换 字符串
print(now.strftime("%Y/%m/%d %H:%M:%S"))
print(now.strftime("%Y-%m-%d %H:%M:%S"))
print(now.strftime("%Y/%m/%d"))
print(now.strftime("%Y-%m-%d"))

一文带你吃透Python中的日期时间模块

(2)字符串 转 datetime对象,通过 strptime()函数。

?
1
2
3
4
dates = ['2022-02-20 20:23:35', '2024-02-20 20:23:35']
datelist = [datetime.strptime(i, "%Y-%m-%d %H:%M:%S") for i in dates]
print(datelist)
# [datetime.datetime(2022, 2, 20, 20, 23, 35), datetime.datetime(2024, 2, 20, 20, 23, 35)]

(3)parse() 解析字符串

将字符串形式的日期解析成 datetime对象。

?
1
2
3
4
5
6
7
8
# 需要安装 pip install python-dateutil
from dateutil.parser import parse
 
print(parse('2/20/2023'))
print(parse('2023-2-20'))
print(parse('2023.2.20'))
print(parse('2023 2 4'))
print(parse('2023, 6, 2'))

一文带你吃透Python中的日期时间模块

3、计算时间间隔

时间间隔 timedelta

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import datetime
 
now = datetime.datetime.now()
print(now)
 
# 3天后
dt1 = now + datetime.timedelta(3)
print(dt1)
 
# 3天前
dt2 = now + datetime.timedelta(-3)
print(dt2)
 
# 3小时30秒后
dt3 = now + datetime.timedelta(hours=3, seconds=30)
print(dt3)

一文带你吃透Python中的日期时间模块

三、calendar模块

calendar模块的函数都是日历相关的,例如打印某月的字符月历。

calendar 特点:

  • calendar 模块是基于datetime.date.weekday()对计算每一周的周数
  • calendar 默认星期一是每一周的第一天,星期天是一周的最后一天
  • calendar 提供calendar.setfirstweeksday()来更改指定星期几为一周的第一天

1、打印日历

?
1
2
3
4
5
6
7
import calendar
 
# 打印指定某年的月历
print(calendar.month(2023, 5))
 
# 打印2023年历
# print(calendar.calendar(2023))

一文带你吃透Python中的日期时间模块

上面示例了一些常见的操作,下面参考文章(感谢博主)中详细列举的各模块之间的属性和方法,这里就不重复造轮子啦。

以上就是一文带你吃透Python中的日期时间模块的详细内容,更多关于Python日期时间模块的资料请关注服务器之家其它相关文章!

原文链接:https://blog.csdn.net/qq_42402854/article/details/129131487

延伸 · 阅读

精彩推荐
  • PythonPython import自己的模块报错问题及解决

    Python import自己的模块报错问题及解决

    这篇文章主要介绍了Python import自己的模块报错问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...

    cacho_379678655272022-09-21
  • Pythonpython 顺时针打印矩阵的超简洁代码

    python 顺时针打印矩阵的超简洁代码

    今天小编就为大家分享一篇python 顺时针打印矩阵的超简洁代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    nillei5042021-04-19
  • PythonPython多线程同步Lock、RLock、Semaphore、Event实例

    Python多线程同步Lock、RLock、Semaphore、Event实例

    这篇文章主要介绍了Python多线程同步Lock、RLock、Semaphore、Event实例,Lock & RLock 用来确保多线程多共享资源的访问,Semaphore用来确保一定资源多线程访问时的上...

    脚本之家4202020-05-14
  • PythonPython使用redis pool的一种单例实现方式

    Python使用redis pool的一种单例实现方式

    这篇文章主要介绍了Python使用redis pool的一种单例实现方式,结合实例形式分析了Python操作redis模块实现共享同一个连接池的相关技巧,需要的朋友可以参考下...

    mo_guang8472020-08-19
  • Pythonpython区块链简易版交易实现示例

    python区块链简易版交易实现示例

    这篇文章主要为大家介绍了python区块链简易版交易实现示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪...

    晓彬_10662023-02-16
  • PythonPython之使用adb shell命令启动应用的方法详解

    Python之使用adb shell命令启动应用的方法详解

    今天小编就为大家分享一篇Python之使用adb shell命令启动应用的方法详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    只为向上7682021-05-12
  • PythonPytest中skip和skipif的具体使用方法

    Pytest中skip和skipif的具体使用方法

    在实际的测试中,我们经常会遇到需要跳过某些测试用例的情况,pytest提供了skip和ifskip来跳过测试.下面我们就来通过一些例子看看skip和ifskip具体如何使用吧...

    RockChe8352021-12-10
  • Pythonpython的常用模块之collections模块详解

    python的常用模块之collections模块详解

    这篇文章主要介绍了python的常用模块之collections模块详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    淡泊明志&&宁静致远5642021-04-26