Python 时间处理
文章目录
简介
时间的处理是各大语言都需要处理的问题,主要在于字符串和时间对象之间的互相转换,用于满足不同场景的需求,那么下面我们就来介绍下在 python 中如何进行转换。
如何格式化日期和时间
在Python中,我们可以使用 datetime 模块的 strftime() 方法来格式化日期和时间
strftime() 方法接受一个格式化字符串作为参数,然后返回格式化后的日期和时间字符串。
下面是一些常用的格式化字符串及其对应的含义:
%Y:4位数的年份(例如:2022)
%m:2位数的月份(01-12)
%d:2位数的日期(01-31)
%H:24小时制的小时(00-23)
%M:分钟(00-59)
%S:秒(00-59)
示例:
import datetime
date_time = datetime.datetime(2022, 1, 26, 11, 43, 7)
formatted_time = date_time.strftime("%Y-%m-%d %H:%M:%S")
print("格式化后的时间:", formatted_time)
将字符串转换为日期时间对象
from datetime import datetime
from dateutil import parser
d1 = "Jan 7 2015 1:15PM"
d2 = "2015 Jan 7 1:33PM"
# If you know date format
date1 = datetime.strptime(d1, '%b %d %Y %I:%M%p')
print(type(date1)) # class 'datetime.datetime'
print(date1) # 2015-01-07 13:15:00
# If you don't know date format
date2 = parser.parse(d2)
print(type(date2)) # class 'datetime.datetime'
print(date2) # 2015-01-07 13:33:00
如何进行日期和时间的计算
在Python中,我们可以使用datetime模块进行日期和时间的计算。 datetime模块提供了一些方便的方法来增加或减少日期和时间,以及计算日期和时间的差值。
下面是一些常用的日期和时间计算方法:
timedelta(days=n):增加或减少n天
timedelta(hours=n):增加或减少n小时
timedelta(minutes=n):增加或减少n分钟
timedelta(seconds=n):增加或减少n秒
下面是一个完整的示例代码,展示了如何进行日期和时间的计算并打印出来:
import datetime
current_time = datetime.datetime.now()
one_day = datetime.timedelta(days=1)
next_day = current_time + one_day
print("当前时间:", current_time)
print("下一天的时间:", next_day)
运行结果如下:
当前时间: 2022-01-26 11:43:07.986973
下一天的时间: 2022-01-27 11:43:07.986973
使用 time 模块展示当前日期和时间
import time
from time import gmtime, strftime
t = time.localtime()
print (time.asctime(t)) # Sun May 7 09:30:37 2017
print(strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())) # Sun, 07 May 2017 04:00:37 +0000
print(strftime("%A", gmtime())) # Sunday
print(strftime("%D", gmtime())) # 05/07/17
print(strftime("%B", gmtime())) # May
print(strftime("%y", gmtime())) # 17
# Convert seconds into GMT date
print(strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime(1234567890))) # Fri, 13 Feb 2009 23:31:30 +0000
以毫秒为单位获取当前时间
import time
milliseconds = int(round(time.time() * 1000))
print(milliseconds) # 1516364270650
计算两个日期时间对象之间的时差
import datetime
from datetime import timedelta
datetimeFormat = '%Y-%m-%d %H:%M:%S.%f'
date1 = '2016-04-16 10:01:28.585'
date2 = '2016-03-10 09:56:28.067'
diff = datetime.datetime.strptime(date1, datetimeFormat)\
- datetime.datetime.strptime(date2, datetimeFormat)
print("Difference:", diff) # Difference: 37 days, 0:05:00.518000
print("Days:", diff.days) # Days: 37
print("Microseconds:", diff.microseconds) # Microseconds: 518000
print("Seconds:", diff.seconds) # Seconds: 300
获取整点时间
from datetime import datetime
# 获取当前时间
current_time = datetime.now()
# 设置分钟和秒钟部分为零
current_time = current_time.replace(minute=0, second=0, microsecond=0)
print("当前整点时间:", current_time)
将时间戳转换为日期和时间
import datetime
timestamp = 1643243787.9869733
date_time = datetime.datetime.fromtimestamp(timestamp)
参考
https://geek-docs.com/python/python-ask-answer/11_hk_1707702624.html#google_vignette https://blog.csdn.net/qdPython/article/details/132778657
文章作者 Brook
上次更新 2024-06-06