Python 如何对日期进行算术运算
本文将介绍如何在Python中对日期进行算术运算。现在我们来看看5个示例:
- 给定日期添加天数
-
给定日期减去天数
-
给定日期添加天数和小时数
-
从当前日期减去月份
-
给定日期添加年份
示例1:给定日期添加天数
步骤
执行所需任务的算法/步骤如下:
- 使用 import 关键字导入 datetime 模块
-
输入日期并创建一个变量来存储它
-
输入日期格式字符串并创建一个变量来存储它
-
使用 strptime() 函数格式化给定日期(将字符串格式的时间戳转换为日期-时间对象)并使用 timedelta() 函数将其加上1天(通常用于计算日期之间的差异。它也可以用于在Python中操作日期,这个函数使用户可以轻松地完成操作)
-
再次使用 strftime() 函数(根据格式代码返回表示日期时间对象的字符串)将新日期格式化,将新日期对象和日期格式作为参数传递给它
-
打印新日期,在旧日期基础上加1天后的结果
以下程序返回给定日期加1后的结果-
# importing datetime module
import datetime
# input date
inputDate = '21-04-2013'
# giving the date format
date_format = '%d-%m-%Y'
# formatting the date using strptime() function and adding 1 day to it
date = datetime.datetime.strptime(inputDate, date_format) + datetime.timedelta(days=1)
# Formatting the date
date=date.strftime(date_format)
# Printing the modified date
print('New Date after incrementing old date {',inputDate,'} by 1 is:',date)
输出
New Date after incrementing old date { 21-04-2013 } by 1 is: 22-04-2013
示例2:在给定的日期上减去天数
这与上一种方法相似,但这次我们将给定的日期减去某个随机数,比如2,如下所示。
以下程序返回给定日期减2得到的结果-
import datetime
# input date
inputDate = '21-04-2013'
# giving the date format
date_format = '%d-%m-%Y'
# formatting the date using strptime() function and subtracting 2 days from it
date = datetime.datetime.strptime(inputDate, date_format) - datetime.timedelta(days=2)
# Formatting the date
date=date.strftime(date_format)
# Printing the modified date
print('New Date after decrementing old date {',inputDate,'} by 2 is:',date)
输出
New Date after decrementing old date { 21-04-2013 } by 2 is: 19-04-2013
示例3:给定日期增加天数和小时数
步骤
以下是执行所需任务的算法/步骤:
- 使用import关键字导入datetime、timedelta模块
-
使用datetime.now()函数输入当前日期和时间(返回当前本地日期和时间)以获取当前日期和时间的HH:MM:SS格式。
-
通过将天数和小时作为参数传递给timedelta()函数将当前日期增加,并将这个新日期存储在一个变量中。
-
打印增加旧日期所得到的新日期,以显示给定天数和小时数的增加。
以下程序返回通过小时和天数增加的给定日期:
# importing datetime, timedelta modules
from datetime import datetime, timedelta
# getting the current date and time
currentDate = datetime.now()
print("The Current Date and Time = ", currentDate)
# Incrementing the current date with some random days and hours
newDate = currentDate + timedelta(days=4, hours=3)
print('New Date:',newDate)
输出
The Current Date and Time = 2022-09-07 02:26:21.098855
New Date: 2022-09-11 05:26:21.098855
示例4:从当前日期中减去月份
步骤
以下是执行所需任务的算法/步骤:
- 使用import关键字导入datetime、relativedelta模块
-
使用datetime()函数输入日期和时间。
-
打印给定的输入日期和时间。
-
通过将月份作为参数传递给 relativedelta() 函数,从给定日期中减去n个月(relativedelta类型旨在应用于现有的datetime,可以用于替换datetime的特定组件或表示时间间隔)。
-
打印从中减去月份后的新日期。
以下程序返回按月份减去的给定日期:
# importing datetime, relativedelta modules
import datetime
from dateutil.relativedelta import relativedelta
# input date and time in (yy,mm,dd,hh,mm,ss)
inputDatetime = datetime.datetime(2019, 9, 12, 8, 40, 7)
# printing the input date and time
print("Input Date and time = ", inputDatetime)
# Subtracting 5 months using the relativedelta function
newDate = inputDatetime - relativedelta(months = 5)
# Printing the modified date
print("5 Months Ago = ", newDate)
输出
Input Date and time = 2019-09-12 08:40:07
5 Months Ago = 2019-04-12 08:40:07
示例5:为给定日期添加年份
该方法与上面的方法相同,但在本例中,我们使用relativedelta()函数来增加给定日期的年份,例如8年。
以下程序返回增加了年份的给定日期-
# importing datetime, relativedelta modules
import datetime
from dateutil.relativedelta import relativedelta
# input date and time in (yy,mm,dd,hh,mm,ss)
inputDatetime = datetime.datetime(2019, 9, 12, 8, 40, 7)
# printing the input date and time
print("Input Date and time = ", inputDatetime)
# Incrementing 8 years using the relativedelta function
newDate = inputDatetime + relativedelta(years = 8)
# Printing the modified date
print("After 8 years = ", newDate)
输出
Input Date and time = 2019-09-12 08:40:07
After 8 years = 2027-09-12 08:40:07
结论
在这篇文章中,我们学习了如何使用五个不同的示例来应用于给定日期的算术操作,比如增加和减少年份、月份、日期和小时数。
极客笔记