Datetime has no attribute datetime - python

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

Related

Python datetime.today() to handle timezone

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'

Change datetime format in django or convert it to default

How do I convert custom datetime value ex. "15:22:03 13/11/2019"
to "13/11/2019 15:22:03" to add value as datetime in models?
Or how do i change default django datetime format?
thank you
You can use this to change the default:
date = models.DateField(blank=False, default=datetime.now().strftime(("%d.%m.%Y %H:%M:%S")))
You can get the % strings at the bottom of this page to make it fit the format you asked for in the column, i can update it for you if you can't get it to work, i'm pretty good with Django.
https://docs.python.org/2/library/datetime.html
You can convert your datetime string to datetime object and insert the date into your database.
from datetime import datetime
date_str = "15:22:03 13/11/2019"
temp_date = datetime.strptime(date_str, "%H:%M:%S %d/%m/%Y").date()
ModelClassName.objects.create(name='ABC', date_time=temp_date)
installed dateutil.parser
in view added
from dateutil.parser import parse as date_parse
and in csv import, in a needed row just added date_parse
reading_date=date_parse(READING_DATE),

How to turn the value of a variable into a datetime object

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")

How to use python date time function/ increment?

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.

Why does trying to use `datetime.strptime` result in " 'module' object has no attribute 'strptime'"? [duplicate]

This question already has answers here:
AttributeError: 'datetime' module has no attribute 'strptime'
(5 answers)
Closed last month.
I'm using strptime to convert a date string into a datetime. According to the linked page, formatting like this should work:
>>> # Using datetime.strptime()
>>> dt = datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M")
My code is:
import datetime
dtDate = datetime.strptime(sDate,"%m/%d/%Y")
where sDate = "07/27/2012". (I understand, from the same page, that %Y is "Year with century as a decimal number.")
I have tried putting the actual value of sDate into the code:
dtDate = datetime.strptime("07/27/2012","%m/%d/%Y")
but this does not work. The error I get is:
AttributeError: 'module' object has no attribute 'strptime'
What am I doing wrong?
You should be using datetime.datetime.strptime. Note that very old versions of Python (2.4 and older) don't have datetime.datetime.strptime; use time.strptime in that case.
You are importing the module datetime, which doesn't have a strptime function.
That module does have a datetime object with that method though:
import datetime
dtDate = datetime.datetime.strptime(sDate, "%m/%d/%Y")
Alternatively you can import the datetime object from the module:
from datetime import datetime
dtDate = datetime.strptime(sDate, "%m/%d/%Y")
Note that the strptime method was added in python 2.5; if you are using an older version use the following code instead:
import datetime, time
dtDate = datetime.datetime(*time.strptime(sDate, "%m/%d/%Y")[:6])
Because datetime is the module. The class is datetime.datetime.
import datetime
dtDate = datetime.datetime.strptime(sDate,"%m/%d/%Y")
You should use strftime static method from datetime class from datetime module. Try:
import datetime
dtDate = datetime.datetime.strptime("07/27/2012", "%m/%d/%Y")
You can also do the following,to import datetime
from datetime import datetime as dt
dt.strptime(date, '%Y-%m-%d')
If in the folder with your project you created a file with the name "datetime.py"

Categories

Resources