close

[Solved] NameError: name ‘datetime’ is not defined

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error NameError: name ‘datetime’ is not defined in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How NameError: name ‘datetime’ is not defined Error Occurs?

Today I get the following error NameError: name ‘datetime’ is not defined in python.

How To Solve NameError: name ‘datetime’ is not defined Error ?

  1. How To Solve NameError: name 'datetime' is not defined Error ?

    To Solve NameError: name 'datetime' is not defined Error You need to import the module datetime first:
    >>> import datetime

  2. NameError: name 'datetime' is not defined

    To Solve NameError: name 'datetime' is not defined Error You need to import the module datetime first:
    >>> import datetime

Solution 1

It can also be used as below:

from datetime import datetime
start_date = datetime(2016,3,1)
end_date = datetime(2016,3,10)

Solution 2

You need to import the module datetime first:

>>> import datetime

After that it works:

>>> import datetime
>>> date = datetime.date.today()
>>> date
datetime.date(2013, 11, 12)

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read