type object 'datetime.datetime' has no attribute 'datetime'
For python 3.3
from datetime import datetime, timedelta
futuredate = datetime.now() + timedelta(days=10)
How to solve "type object 'datetime.datetime' has no attribute 'timedelta'" when creating a new date?
You've imported the wrong thing; you've done from datetime import datetime
so that datetime
now refers to the class, not the containing module.
Either do:
import datetime
...article.created_on + datetime.timedelta(...)
or
from datetime import datetime, timedelta
...article.created_on + timedelta(...)
Python's datetime: "AttributeError: type object 'datetime.date' has no attribute 'strptime'"
There indeed is no strptime()
for a datetime.date
object, and there is no strpdate()
function either.
Just use datetime.strptime()
and convert to a date object afterwards:
my_date = datetime.datetime.strptime(date_str, "%d-%m-%Y").date()
type object 'datetime.datetime' has no attribute 'fromisoformat'
The issue here is actually that fromisoformat
is not available in Python versions older than 3.7, you can see that clearly stated in the documenation here.
Return a date corresponding to a date_string given in the format YYYY-MM-DD:
>>>
>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
This is the inverse of date.isoformat(). It only supports the format YYYY-MM-DD.
New in version 3.7.
AttributeError while using timedelta: type object 'datetime.datetime' has no attribute 'datetime'
You are importing datetime
from datetime
. Later in the code you using datetime.datetime
, so that's giving you error.
You shoud just call t = datetime(*struct_time[:6])
or
just do import datetime
and call it t = datetime.datetime(*struct_time[:6])
The correct program should look like:
import time
import datetime
time_string = '2018-07-16T23:50:55+0000'
#Reduct 8 hours and print in human readable format
struct_time = time.strptime(time_string, "%Y-%m-%dT%H:%M:%S+0000")
t = datetime.datetime(*struct_time[:6])
delta = datetime.timedelta(hours=8)
print(t+delta)
Related Topics
Find the Index of the First Digit in a String
Pandas Convert from Datetime to Integer Timestamp
How to Get One Key and Value from a Json in Python
How to Find a Minimum Value in a 2D Array Without Using Numpy or Flattened in Python
How to Convert Data from Txt Files to Excel Files Using Python
Reading a CSV File into Pandas Dataframe With Quotation in Some Entries
Easiest Way to Convert Two Columns to Python Dictionary
How to Name a File by a Variable Name in Python
Can Anyone Explain Me What This Python 3 Command Do
Importing Local Module (Python Script) in Airflow Dag
Copy All Values in a Column to a New Column in a Pandas Dataframe
How to Stop Execution of Python Script in Visual Studio Code
How to Extract Column Value Within Square Brackets in Pyspark
Getting All Possible Combinations from a List With Duplicate Elements
How to Uniqify a List of Dict in Python