I got a python script, which is supposed to fetch data of my mysql table (contains a date) and then it should print out the amount of days between today´s date and the date of my mysql table.

I already tried substracting the two dates, but this wouldn´t work. So I tried substracting today´s date and my birthday, which worked. So I think the problem is the mysql date.

import datetime
import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="",
  database='appointments'
)

eventDate = mydb.cursor()
eventDate.execute('SELECT event_date FROM appointdatetitle')
Date = eventDate.fetchall()

tdelta = datetime.timedelta(days=7)
today = datetime.date.today()
eday = Date
till_eday = eday - today
print(till_eday.days)

1 Answers

1
Devesh Kumar Singh On Best Solutions

Date = eventDate.fetchall() fetchall actually fetches a tuple of tuples e.g.
(('abcd',), ('efgh',))

You would want to get the Date by looping over the list and getting the elements. e.g.

for d in Date:
    #This will contain the date you need
    print(d[0])

Or you can directly access an element by doing say Date[0][0]

After update from OP, this is what can be done!
OP can use https://dateutil.readthedocs.io/en/stable/index.html to use the
from dateutil.relativedelta import * to increment the month!

import datetime
from dateutil.relativedelta import *

Date = [(datetime.date(2019, 4, 24),)]

#Extract actual datetime object from Date variable
date_obj = Date[0][0]

#Increment month by 1 since January starts from 0
date_obj += relativedelta(months=1)

today = datetime.date.today()

#Using datetime from above
eday = date_obj
till_eday = eday - today
print(till_eday.days)

The output will be 23