📜  如何在Python中使用 DateTime 添加和减去天数?

📅  最后修改于: 2022-05-13 01:55:01.895000             🧑  作者: Mango

如何在Python中使用 DateTime 添加和减去天数?

我们知道日期和时间用于我们必须跟踪日期和时间的程序中,因此有必要有一个模块来操作日期和时间。在Python中,日期时间模块处理日期和时间。日期时间模块内置于Python标准库中。

Datetime 模块由以下类组成:

 

Name

Description

1.dateIt shows the date according to the Georgian calendar with attributes are year, month and day.
2.timeIt shows time, independent of any particular day with attributes are hour, minute, second, microsecond, and tzinfo.
3.datetimeIt is a collection of date and time with the attributes year, month, day, hour, minute, second, microsecond, and tzinfo.
4.timedeltaIt is used to manipulate date.
5.tzinfoIt provides information about time zone.

在Python中使用 DateTime 添加和减去天数

为了添加或减去日期,我们使用称为timedelta()函数的东西,它可以在datetime类下找到。它用于操作日期,我们可以对日期进行算术运算,例如加法或减法。 timedelta实现起来非常容易和有用。

示例 1.添加天数

Python3
# MANIPULATING DATETIME
from datetime import date, timedelta
 
today_date = date.today()
 
print("CURRENT DAY : ", today_date)
 
# as said earlier it takes argument as day by default
td = timedelta(5)
print("AFTER 5 DAYS DATE WILL BE : ", today_date + td)


Python3
# MANIPULATING DATETIME
from datetime import date, timedelta
 
current_date = date.today()
 
print("CURRENT DAY : ",current_date)
 
print("OLD Date : ",current_date - timedelta(17))


Python3
# Manipulate DATETIME
from datetime import datetime, timedelta
current = datetime.now()
print("This is the current date and time :- ", current)
 
# FOR PRINTING TOMORROW'S DATE
tomorrow = timedelta(1)
print("Tomorrow's date and time :- ", current + tomorrow)
 
# FOR PRINTING YESTERDAY'S DATE
yesterday = timedelta(-1)
print("Yesterday's date and time :- ", current + yesterday)


Python3
# MANIPULATING DATETIME
from datetime import datetime, timedelta
 
curr = datetime.now()
print("Current Date and time :- ", curr)
 
new_datetime = timedelta(days = 10, seconds = 40,
                         microseconds = 10,
                         milliseconds = 60,
                         minutes = 10, hours = 4,
                         weeks = 8)
 
print("New Date and time :- ", curr + new_datetime)


输出:

CURRENT DAY :  2020-12-27
AFTER 5 DAYS DATE WILL BE :  2021-01-01

示例 2.减去天数

蟒蛇3

# MANIPULATING DATETIME
from datetime import date, timedelta
 
current_date = date.today()
 
print("CURRENT DAY : ",current_date)
 
print("OLD Date : ",current_date - timedelta(17))


输出:

CURRENT DAY :  2020-12-27
OLD Date :  2020-12-10

在上面的代码中,我创建了一个名为current_date的变量,它保存当前日期,然后打印当前日期。

之后,我使用了 timedelta函数,在参数中,我们传递了一个值,表示要添加或减去多少天(该值可以是任何整数)。

同样,我们也可以用时间做同样的事情。

示例 3:

蟒蛇3

# Manipulate DATETIME
from datetime import datetime, timedelta
current = datetime.now()
print("This is the current date and time :- ", current)
 
# FOR PRINTING TOMORROW'S DATE
tomorrow = timedelta(1)
print("Tomorrow's date and time :- ", current + tomorrow)
 
# FOR PRINTING YESTERDAY'S DATE
yesterday = timedelta(-1)
print("Yesterday's date and time :- ", current + yesterday)


输出:

This is the current date and time :-  2020-12-27 13:50:14.229336
Tomorrow's date and time :-  2020-12-28 13:50:14.229336
Yesterday's date and time :-  2020-12-26 13:50:14.229336

示例 4:

蟒蛇3

# MANIPULATING DATETIME
from datetime import datetime, timedelta
 
curr = datetime.now()
print("Current Date and time :- ", curr)
 
new_datetime = timedelta(days = 10, seconds = 40,
                         microseconds = 10,
                         milliseconds = 60,
                         minutes = 10, hours = 4,
                         weeks = 8)
 
print("New Date and time :- ", curr + new_datetime)

输出:

Current Date and time :-  2020-12-27 13:58:42.178211
New Date and time :-  2021-03-03 18:09:22.238221