Home > Back-end >  Why I'm getting error like this while printing today's date in python?
Why I'm getting error like this while printing today's date in python?

Time:05-16

This is my imported libraries and code_

import time
import datetime
from datetime import datetime

exp_list = data['records']['expiryDates']
exp_date = exp_list[1]
exp_date = datetime.strptime(exp_date, "%d %b %Y") # Convert the type date object into datetime format
today = datetime.date.today() #Today's date
delta = exp_date - today
print(delta.days)

When I trying to run this code I'm getting error like this_

      39 print(exp_date)
      40 
 ---> 41 today = datetime.date.today() #Today's date
      42 delta = exp_date - today
      43 print(delta.days)

      AttributeError: 'method_descriptor' object has no attribute 'today'

CodePudding user response:

from datetime import datetime


today = datetime.today() #Today's date
print(today)


>> 2022-05-16 09:57:12.964475

CodePudding user response:

If you like to use "datetime.date.today()", just import "datetime" module .E.g

import datetime

datetime.date.today()"

Output:

datetime.date(2022, 5, 16)

or, you can use

from datetime import date

date.today()

Output:

datetime.date(2022, 5, 16)

CodePudding user response:

import datetime

today = datetime.datetime.today() # 16 May 2022
exp_date = datetime.datetime(2022, 5, 28, 9, 56) # 28 May 2022
delta = exp_date - today

print(delta.days) #12
  • Related