I read many of the similar questions, the function is simple nested for loops used for the API, each minute I can call 5 times. So I set the range of 75 for one year data. Can you guys help me solve the problem? Thanks in advance!
The first part is working, enter the zip code from the list.
for zip in zipcode:
url4 = url1+str(zip)+url2
Second part is not working
for x in range (0,75):
counter = x * 5
startdate += datetime.timedelta(days=counter)
enddate += datetime.timedelta(days=counter)
url = url4+startdate+","+enddate+url3
apifunction(url)
system said:
Traceback (most recent call last):
File "<pyshell#51>", line 3, in <module>
startdate += datetime.timedelta(days=counter)
AttributeError: type object 'datetime.datetime' has no attribute 'timedelta'
Probably you're importing like this
from datetime import datetime
This way you import datetime type from datetime module. And this type of course does not have timedelta type in it.
You should import like this:
import datetime
In this case you simply import datetime module, and when you do datetime.timedelta it means that you want to use type timedelta from module datetime
It looks like datetime is getting redefined somewhere else in your code.
>>> import datetime
>>> type(datetime)
<type 'module'>
Your 'datetime' there is an object, which probably means you made a variable and called it datetime somewhere else in your code.
Related
Hi apologies on basic python datetime question but I am a little confused:
I want to just have a variable the prints today's date, with consideration to the time zone the program I am running it in. Let's say California.
import datetime
import pytz
utc_now = pytz.utc.localize(datetime.datetime.utcnow())
pst_now = utc_now.astimezone(pytz.timezone("America/Los_Angeles"))
x = pst_now.isoformat()
for x it returns :
2020-01-13T17:43:56.155556-08:00
how can I get it to return:
2020-01-13
I tried:
datetime.datetime.strptime(x, '%Y-%m-%d)
But it did not work
If you're just looking to return the time of the local machine, no need to deal with timezones directly in your code, you can use the now function of datetime.
import datetime
datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d')
x is a string. pst_now is a datetime object which, when the method .isoformat() is called on it, produces a string.
Solution: call strftime on pst_now:
x = pst_now.strftime('%Y-%m-%d')
You can convert pst_now to a date() object:
pst_now.date().isoformat()
'2020-01-13'
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'
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
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
I have an input in tkinter where a date is entered in the format of dd/mm/yyyy . I would like to make a variable equal to the three letter version of the month and another to be equal to the yyyy of a month.
the input variable is date_entry and I've tried
ss_date = date_entry.strftime("%d/%m/%Y")
and
ss_date = (datetime.strptime(date_entry, "%d/%m%Y"))
but i keep getting module 'datetime' has no attribute 'strptime'
I've looked online but every example uses datetime to generate an example in the first place, I cannot find any explanation of how to extract it from a variable value.
```
ss_date = date_entry.strftime("%d/%m/%Y") and
ss_date = (datetime.strptime(date_entry, "%d/%m%Y"))
```
module 'datetime' has no attribute 'strptime'
Can you please help.
Please make sure you import datetime correctly. Python has a library datetime which then also has a datetime object in it. So either import the object version directly, or call it by the 'full name' (datetime.datetime).
Object version:
from datetime import datetime
date_entry = "04/05/2019"
datetime.strptime(date_entry, "%d/%m/%Y")
'Full name' version:
import datetime
date_entry = "04/05/2019"
datetime.datetime.strptime(date_entry, "%d/%m/%Y")
I want to return datetime.datetime.strptime(date_visit,"%Y-%m-%d") into my function read_file but python keeps saying that datetime.datetime has no attribute datetime? In the function where I return it´s working. Pls help anyone.
return datetime.datetime.strptime(date_visit,"%Y-%m-%d")
def read_file(date):
all_animals = list()
day = datetime.datetime.strptime(date_visit,"%Y-%m-%d").isoweekday()
datetime.datetime.strptime(date_visit,"%Y-%m-%d")
workingday = [1,2,3,4,5]
Just replace import datetime.datetime by import datetime
Of course datetime.datetime does not have the attribute datetime. You should use it like this:
from datetime import datetime
datetime.strptime(...)
Or:
import datetime
datetime.datetime.strptime(...)
To use datetime.datetime.strptime() you need to import the whole module:
import datetime
The second datetime is a type in the first one, and it has strptime() function. For more details please check this link:
datetime — Basic date and time types
I'm trying to retrieving free/busy status from outlook calender for particular person using python language.
here is my code for it.
import win32com.client
obj_outlook = win32com.client.Dispatch('Outlook.Application')
obj_Namespace = obj_outlook.GetNamespace("MAPI")
obj_Recipient = obj_Namespace.CreateRecipient("someone#domain.com")
str_Free_Busy_Data = obj_Recipient.FreeBusy("11-11-2013", 11)
print str_Free_Busy_Data
but I'm getting an error:
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
str_Free_Busy_Data = obj_Recipient.FreeBusy("11-11-2013", 11)
File "<COMObject CreateRecipient>", line 4, in FreeBusy
TypeError: an integer is required
So my question is Recipient.FreeBusy() method takes two mandatory arguments, Start Date and duration. Here 11 is the duration, which is an Integer. So why python is not able to identify the integer argument here and returning an TypeError.
Please help me in case I have done anything wrong (I'm still a newbie in python world).
Thanks in advance.
I looked up the method in MSDN.
http://msdn.microsoft.com/en-us/library/office/microsoft.office.interop.outlook.recipient.freebusy(v=office.12).aspx
The syntax for the method takes 3 arguments.
string FreeBusy(
DateTime Start,
int MinPerChar,
Object CompleteFormat
)
The issue is that you're passing a string to the DateTime parameter. Instead you need to import the datetime library in your code and use a date parameter.
So, at the start of your code, try this.
import datetime
#Then declare the my_date variable as datetime.date.
my_date = datetime.date(2013,11,23)
str_Free_Busy_Data = obj_Recipient.FreeBusy(my_date, 11)
The first parameter to FreeBusy is a Date object. Pywin won't convert a string into a Date , but it can convert a pywintypes.Time object, or an integer representing the number of seconds since the Unix epoch. Hence the error: When the first argument is implicitly converted to a Time, the constructor complains that it needs an integer.
#start date: 12/31/1969 7:00:00 PM
str_Free_Busy_Data = obj_Recipient.FreeBusy(0, 11)
There are a number of ways to get the Unix timestamp from a date. See Convert python datetime to epoch with strftime.