Tzbliss
Tzbliss

Reputation: 5

is there any solution for Datetime error in python?

I need help in my code as I'm new in python. I'm using the DateTime library in my code to know the current datetime and doy1 for finding the day of the year.

I tried using python 3.6 idle with different modules and after that, I used Visual Studio community 2017(just for my satisfaction) but it showing me an error. I know it's not tool issue but I just tried.

import datetime
from dateutil import parser
from datetime import datetime

ask=input("enter date\n")
date_format = "%Y-%m-%d"
date_time = datetime.strptime(ask, date_format)
Current_date = datetime.strptime((str(datetime.now().date())), date_format)

print(Current_date)

doy1=date_time.strftime("%j")# day of year

  date=datetime.now()
  doy2=date.strftime("%j")
  if(doy1<doy2):
    diff_of_dates=abs(int(doy1)-int(doy2))
    print(diff_of_dates)
    diff=diff_of_dates+1
    for i in range(1,diff):
      avg_20=int(doy1)+1
      print(doy1)
      temp_date=datetime.date(date_format)+datetime.timedelta(doy1-1)
      print("Difference of day",temp_date)


#ERROR
Traceback (most recent call last):
  File "C:\Users\Muahr\source\repos\RCAI-Project\Pest\temperature.py", line 157, in <module>
    temp_date=datetime.date(date_format)+datetime.timedelta(doy1-1)
TypeError: descriptor 'date' requires a 'datetime.datetime' object but received a 'str'

Upvotes: 0

Views: 1787

Answers (2)

S Raghav
S Raghav

Reputation: 1526

I understand that you are trying to get the difference of two dates and print the intermediate dates one day at a time.

I removed certain sections of your code to simplify it and came up with this code that works

EDIT: this code assumes that the user entered date is always older than the current date. You can update the logic to check for the larger date and find the difference accordingly

import datetime
from datetime import datetime
from datetime import timedelta

ask=input("enter date\n")
date_format = "%Y-%m-%d"
day1 = datetime.strptime(ask, date_format)
day2 = datetime.strptime((str(datetime.now().date())), date_format)

diff_of_dates = day2 - day1
diff=diff_of_dates.days
for i in range(1,diff): 
    temp_date=day1+timedelta(days=i)
    print("Difference of day",datetime.strftime(temp_date, date_format))

Output

> python test.py
enter date
2019-03-28
Difference of day 2019-03-29
Difference of day 2019-03-30
Difference of day 2019-03-31

Upvotes: 0

Slo Learner
Slo Learner

Reputation: 58

I think I solved some of the problem with the below code. basically, you were putting a string in the datetime.date() object. The string you were using was the date_format variable, which defined the date_time variable on the next line. I put the date_time variable when you're assigning the temp_date and that error went away.

There is a secondary issue I found with the way you were calling timedelta, I took off the datetime prefix and imported timedelta, which resolved that. The code below runs, but it's not performing the calculations you choose when calling timedelta.

temp_date=datetime.date(date_time)+timedelta()

Changed the class import line as well:

from datetime import datetime, timedelta

Upvotes: 1

Related Questions