官术网_书友最值得收藏!

Operations on datetime objects

The datetime and timedelta classes support various mathematical operations to get dates in the future or the past. Using these operations returns another datetime object. . In this recipe, you would create datetime, date, time, and timedelta objects and perform mathematical operations on them.

How to do it…

Follow along with these steps to execute this recipe:

  1. Import the necessary modules from the Python standard library:
>>> from datetime import datetime, timedelta
  1. Fetch today's date. Assign it to date_today and print it:
>>> date_today = date.today()              
>>> print(f"Today's Date: {date_today}")

We get the following output. Your output may differ:

Today's Date: 2020-08-12
  1. Add 5 days to today's date using a timedelta object. Assign it to date_5days_later and print it:
>>> date_5days_later = date_today + timedelta(days=5)
>>> print(f"Date 5 days later: {date_5days_later}")

We get the following output. Your output may differ:

Date 5 days later: 2020-08-17
  1. Subtract 5 days from today's date using a timedelta object. Assign it to date_5days_ago and print it:
>>> date_5days_ago = date_today - timedelta(days=5)
>>> print(f"Date 5 days ago: {date_5days_ago}")

We get the following output. Your output may differ:

Date 5 days ago: 2020-08-07
  1. Compare date_5days_later with date_5days_ago using the > operator:
>>> date_5days_later > date_5days_ago

We get the following output:

True
  1. Compare date_5days_later with date_5days_ago using the < operator:
>>> date_5days_later < date_5days_ago

We get the following output:

False
  1. Compare date_5days_later, date_today and date_5days_ago together using the > operator:
>>> date_5days_later > date_today > date_5days_ago

We get the following output:

True
  1. Fetch the current timestamp. Assign it to current_timestamp:
>>> current_timestamp = datetime.now()
  1. Fetch the current time. Assign it to time_now and print it:
>>> time_now = current_timestamp.time()
>>> print(f"Time now: {time_now}")

We get the following output. Your output may differ:

Time now: 20:55:45.239177
  1. Add 5 minutes to the current time using a timedelta object. Assign it to time_5minutes_later and print it:
>>> time_5minutes_later = (current_timestamp + 
timedelta(minutes=5)).time()
>>> print(f"Time 5 minutes later: {time_5minutes_later}")

We get the following output. Your output may differ:

Time 5 minutes later: 21:00:45.239177
  1. Subtract 5 minutes from the current time using a timedelta object. Assign it to time_5minutes_ago and print it:
>>> time_5minutes_ago = (current_timestamp - 
timedelta(minutes=5)).time()
>>> print(f"Time 5 minutes ago: {time_5minutes_ago}")

We get the following output. Your output may differ:

Time 5 minutes ago: 20:50:45.239177
  1. Compare time_5minutes_later with time_5minutes_ago using the < operator:
>>> time_5minutes_later < time_5minutes_ago

We get the following output. Your output may differ:

False
  1. Compare time_5minutes_later with time_5minutes_ago using the > operator:
>>> time_5minutes_later > time_5minutes_ago

We get the following output. Your output may differ:

True
  1. Compare time_5minutes_later, time_now and time_5minutes_ago together using the > operator:
>> time_5minutes_later > time_now > time_5minutes_ago

We get the following output. Your output may differ:

True

How it works…

In step 1, you import date, datetime, and timedelta classes from the datetime module. In step 2, you fetch today's date using the today() classmethod provided by the class date and assign it to a new attribute, date_today. (A classmethod allows you to call a method directly on a class without creating an instance.) The return object is of type datetime.date. In step 3, you create a date, 5 days ahead of today, by adding a timedelta object, holding a duration of 5 days, to date_today. You assign this to a new attribute, date_5days_later. Similarly, in step 4, you create a date, 5 days ago and assign it to a new attribute date_5days_ago.

In step 5 and step 6, you compare date_5days_later and date_5days_ago using the > and < operators, respectively. The > operator returns True if the first operand holds a date ahead of that held by operand 2. Similarly, the < operator returns True if the second operand holds a date ahead of that held by operand 1. In step 7, you compare together all three date objects created so far. Note the outputs.

Step 8 to step 14 perform the same operations as step 2 to step 7, but this time on datetime.time objects—fetching current time, fetching a time 5 minutes ahead of the current time, fetching a time 5 minutes before the current time and comparing all the datetime.time objects which are created. The timedelta objects cannot be added to datetime.time objects directly to get time in the past or the future. To overcome this, you can add timedelta objects to datetime objects and then extract time from them using the time() method. You do this in step 10 and step 11.

There's more

The operations shown in this recipe on date and time objects can similarly be performed on datetime objects. Besides +, -, < and >, you can also use the following operators on datetime, date, and time objects:

This is not an exhaustive list of permissible operators. Refer to the official documentation on datetime module for more information: https://docs.python.org/3.8/library/datetime.html.

主站蜘蛛池模板: 综艺| 景洪市| 社旗县| 邓州市| 滨州市| 永城市| 肇源县| 三门县| 娱乐| 西昌市| 石首市| 屏南县| 苏尼特左旗| 屏山县| 五峰| 尼木县| 广州市| 五台县| 青神县| 齐齐哈尔市| 长宁区| 巴南区| 巴林右旗| 灵寿县| 壤塘县| 静安区| 临武县| 康保县| 宝丰县| 来宾市| 天气| 科尔| 巴彦县| 鹤庆县| 吉木萨尔县| 蓬溪县| 普洱| 平泉县| 保康县| 武乡县| 宿州市|