Python datetime.timedelta.timedelta Days Attribute

Vaibhav Vaibhav Jan 30, 2023
  1. Syntax
  2. Example Code: Use the datetime.timedelta.timedelta.days Attribute in Python
Python datetime.timedelta.timedelta Days Attribute

A timedelta refers to the difference between two times. This value can be expressed in various units such as minutes, seconds, hours, days, months, weeks, milliseconds, nanoseconds, etc.

The Python programming language contains a module datetime with various classes and procedures to efficiently work with quantities such as time, date, date with time, timezones, etc. It also offers a class timedelta that makes computing this difference seamless.

Using these class instances, we can perform various mathematical operations such as addition, subtraction, multiplication, division, modulo division, negation, etc.

This class also offers attributes that store all the basic values related to the timedelta, such as minutes, hours, and days. In this article, we will talk about one such attribute, days.

To learn more about the timedelta class in-depth and understand what and how mathematical operations are performed over the timedelta class instances, refer to the official Python documentation here.

Syntax

<timedelta-object > .days

Parameters

Since days is an attribute, not a method, it doesn’t accept any parameters.

Returns

Since days is an attribute, not a method, it doesn’t return anything. It stores the value of days in a timedelta object.

Example Code: Use the datetime.timedelta.timedelta.days Attribute in Python

import datetime

delta = datetime.timedelta(
    days=25,
    seconds=33,
    microseconds=10,
    milliseconds=39020,
    minutes=54,
    hours=22,
    weeks=3,
)
print("TimeDelta:", delta)
print("Days:", delta.days)

Output:

TimeDelta: 46 days, 22:55:12.020010
Days: 46

The output shows that the value for days is 46. It is because the time delta has 25 days and 3 weeks, according to which 25 + (3 * 7) = 46.

Vaibhav Vaibhav avatar Vaibhav Vaibhav avatar

Vaibhav is an artificial intelligence and cloud computing stan. He likes to build end-to-end full-stack web and mobile applications. Besides computer science and technology, he loves playing cricket and badminton, going on bike rides, and doodling.

Related Article - Python Datetime