OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Difficulties formatting relativedelta

  • Thread starter Thread starter user25689472
  • Start date Start date
U

user25689472

Guest
I am quite a beginner programmer. I am trying to code a to-do list.

My problem is that when I want to do a "time until" section of the to-do list, getting the time until is my problem

Code:
    #Set current and due date
    deadline = f"{year}-{month}-{day} {hour}:{minute1}:{seconds}"
    timenow = datetime.now().strftime('%Y-%m-%d %H:%M:%S')

    #Format dates
    time_now = datetime.strptime(timenow, '%Y-%m-%d %H:%M:%S')
    final_deadline = datetime.strptime(deadline, '%Y-%m-%d %H:%M:%S')

    diff = final_deadline - time_now

    dates = relativedelta(time_now, final_deadline)

    print(dates)

My problem is when I print relativedelta it comes out like this for example:

relativedelta(months=-1, days=-2, hours=-1, minutes=-48, seconds=-30)

I want to format it to say 1 month, 2 days, 1 hour, 48 minutes and 30 seconds.

I have tried to use strftime and strptime but they both say they are incompatible with relativedelta.
<p>I am quite a beginner programmer. I am trying to code a to-do list.</p>
<p>My problem is that when I want to do a "time until" section of the to-do list, getting the time until is my problem</p>
<pre><code> #Set current and due date
deadline = f"{year}-{month}-{day} {hour}:{minute1}:{seconds}"
timenow = datetime.now().strftime('%Y-%m-%d %H:%M:%S')

#Format dates
time_now = datetime.strptime(timenow, '%Y-%m-%d %H:%M:%S')
final_deadline = datetime.strptime(deadline, '%Y-%m-%d %H:%M:%S')

diff = final_deadline - time_now

dates = relativedelta(time_now, final_deadline)

print(dates)
</code></pre>
<p>My problem is when I print relativedelta it comes out like this for example:</p>
<p>relativedelta(months=-1, days=-2, hours=-1, minutes=-48, seconds=-30)</p>
<p>I want to format it to say 1 month, 2 days, 1 hour, 48 minutes and 30 seconds.</p>
<p>I have tried to use strftime and strptime but they both say they are incompatible with relativedelta.</p>
 

Latest posts

A
Replies
0
Views
1
AgencyAnalytics
A
S
Replies
0
Views
1
Stacker Media
S
C
Replies
0
Views
1
CC.Talent
C
Top