如何在 Python 中对日期执行算术运算?

pythonserver side programmingprogramming更新于 2025/5/8 11:56:17

在本文中,我们将向您展示如何在 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() 函数(relativedelta 类型旨在应用于现有日期时间,可用于替换该日期时间的特定组件或表示时间间隔),从给定日期中减去 n 个月(例如 5 个月)。

  • 从给定日期中减去月份后打印新日期它。

以下程序返回给定日期减去月份的结果−

# 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

结论

在本文中,我们学习了如何使用五个不同的示例对给定的日期应用算术运算,例如增加和减少年、月、日和小时。


相关文章


有用资源