当前位置:
首页 > Python基础教程 >
-
关于Python时间日期常见的一些操作方法
Python的datetime模块是处理日期和时间的强大工具,datetime类可以获取当前时间、指定日期、计算时间差、访问时间属性及格式化时间,这些功能使得在Python中进行时间日期处理变得简单高效,需要的朋友可以参考下
前言
在Python中,我们用于处理时间和日期相关的类型最常用的模块是datetime模块。该模块提供了很多与时间日期相关的类,对我们处理时间日期变得很方便。
以下是一些常见的关于时间日期的操作。
一、datetime类
1、获取当前日期和时间(年、月、日、时、分、秒、微秒)
from datetime import datetime
today = datetime.today()
now = datetime.now()
print("当前日期和时间是:", today) # 当前日期和时间是: 2024-07-29 21:05:42.281563
print("当前日期和时间是:", now) # 当前日期和时间是: 2024-07-29 21:05:42.281563
2、 输出指定的日期
specific_date = datetime(2024, 7, 29)
specific_date1 = datetime(2024, 7, 30, 21, 55, 00)
print("指定日期是:", specific_date) # 指定日期是: 2024-07-29 00:00:00
print("指定日期是:", specific_date1) # 指定日期是: 2024-07-30 21:55:00
3、计算时间差
# 两个日期相减会得到时间差对象(timedelta)
delta = specific_date1 - specific_date
print(delta, type(delta)) # 1 day, 21:55:00 <class 'datetime.timedelta'>
# 获取两个日期相差的天数和秒数
print(delta.days, delta.seconds) # 1 78900
4、访问datetime对象的属性
# 通过datetime对象的属性,单独获取时间的年月日时分秒
year = now.year
month = now.month
day = now.day
hour = now.hour
minute = now.minute
second = now.second
print(f"年: {year}, 月: {month}, 日: {day}, 时: {hour}, 分: {minute}, 秒: {second}")
# 输出->年: 2024, 月: 7, 日: 29, 时: 21, 分: 08, 秒: 40
5、格式化时间
# 格式化时间对象
formatted_datetime = now.strftime('%Y年%m月%d日 %H时%M分%S秒')
print("格式化时间:", formatted_datetime) # 2024年07月29日 21时08分19秒
二、date类
date类一般用于处理日期(年、月、日)。
1、获取当前的日期(年、月、日)和属性
from datetime import date
today1 = date.today()
year = today1.year
month = today1.month
day = today1.day
print(today1) # 2024-07-29
print(f"年: {year}, 月: {month}, 日: {day}") # 年: 2024, 月: 7, 日: 29
三、time类
time类主要用于处理时间(时、分、秒、微秒)。
1、指定时间
from datetime import time
current_time = time(15, 48, 6) # 假设当前时间是15时48分6秒
print("当前时间:", current_time) # 当前时间: 15:48:06
2、通过访问time属性分别获取时、分、秒、微秒
precise_time = time(15, 48, 6, 123456)
print("精确时间:", precise_time)
hour = current_time.hour
minute = current_time.minute
second = current_time.second
microsecond = precise_time.microsecond
print(f"时: {hour}, 分: {minute}, 秒: {second}, 微秒: {microsecond}") # 时: 15, 分: 48, 秒: 6, 微秒: 123456
四、timedelta类
1、计算过去未来的日期
from datetime import timedelta
# 计算未来三天的日期
future_date = now + timedelta(days=3)
print("三天后的日期:", future_date) # 三天后的日期: 2024-08-01 21:16:26.496122
# 计算过去一小时的时间
past_time = now - timedelta(hours=1)
print("过去1小时时间:", past_time) # 过去1小时时间:2024-07-28 20:16:26.496122
2、使用多个参数创建timedelta对象
delta = timedelta(weeks=1, days=1, hours=1, minutes=1, seconds=1, microseconds=1)
print("时间:", delta) # 时间: 8 days, 1:01:01.000001
总结
到此这篇关于Python时间日期常见的一些操作方法的文章就介绍到这了,更多相关Python时间日期操作内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
原文链接:https://blog.csdn.net/mr_five55/article/details/140782550
栏目列表
最新更新
求1000阶乘的结果末尾有多少个0
详解MyBatis延迟加载是如何实现的
IDEA 控制台中文乱码4种解决方案
SpringBoot中版本兼容性处理的实现示例
Spring的IOC解决程序耦合的实现
详解Spring多数据源如何切换
Java报错:UnsupportedOperationException in Col
使用Spring Batch实现批处理任务的详细教程
java中怎么将多个音频文件拼接合成一个
SpringBoot整合ES多个精确值查询 terms功能实
SQL Server 中的数据类型隐式转换问题
SQL Server中T-SQL 数据类型转换详解
sqlserver 数据类型转换小实验
SQL Server数据类型转换方法
SQL Server 2017无法连接到服务器的问题解决
SQLServer地址搜索性能优化
Sql Server查询性能优化之不可小觑的书签查
SQL Server数据库的高性能优化经验总结
SQL SERVER性能优化综述(很好的总结,不要错
开启SQLSERVER数据库缓存依赖优化网站性能
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比