datetime.now() Explained

datetime.now() gives you the current local date and time in Python.

Beginners often use it when they want to:

  • record the current time
  • print today’s date
  • create timestamps
  • work with dates and times in programs

It is important to know that datetime.now() returns a datetime object, not a plain string. That means you can access parts of it like the year, month, day, or hour.

from datetime import datetime

now = datetime.now()
print(now)
print(now.year)
print(now.month)
print(now.day)

Use this when you want the current local date and time as a datetime object.

What datetime.now() does #

datetime.now():

  • returns the current local date and time
  • belongs to the datetime class in the datetime module
  • returns a datetime object
  • is useful when you need the current timestamp in your program

A common beginner mistake is thinking it returns text. It does not. It returns an object with useful attributes and methods.

If you want a broader introduction to working with dates and times, see the Python datetime module overview.

Importing datetime.now() correctly #

The most common beginner import is:

from datetime import datetime

Then you call:

datetime.now()

Example #

from datetime import datetime

now = datetime.now()
print(now)

You can also import the whole module:

import datetime

now = datetime.datetime.now()
print(now)

This version works too, but it is longer.

Why beginners get confused #

If you write this:

import datetime

print(datetime.now())

you will get an error, because datetime here is the module, not the class.

You need:

import datetime

print(datetime.datetime.now())

If you run into this kind of problem, see AttributeError: module has no attribute fix.

What the returned object contains #

The object returned by datetime.now() includes:

  • year
  • month
  • day
  • hour
  • minute
  • second
  • microsecond

You can access these parts with attributes.

Example #

from datetime import datetime

now = datetime.now()

print(now.year)
print(now.month)
print(now.day)
print(now.hour)
print(now.minute)
print(now.second)

You can also check its type:

from datetime import datetime

now = datetime.now()
print(type(now))

Expected output will look like:

<class 'datetime.datetime'>

If you want to understand type() better, see Python type() function explained.

Basic example #

Here is a simple example that gets the current date and time, prints it, and accesses one part of it.

from datetime import datetime

now = datetime.now()

print("Full value:", now)
print("Year:", now.year)
print("Hour:", now.hour)

Example output:

Full value: 2026-04-22 14:35:10.123456
Year: 2026
Hour: 14

The exact output will be different on your computer because it uses your current local time.

Getting only the date or only the time #

Sometimes the full datetime value is more than you need.

You can use:

  • .date() to get only the date
  • .time() to get only the time

Example #

from datetime import datetime

now = datetime.now()

print("Full datetime:", now)
print("Date only:", now.date())
print("Time only:", now.time())

Example output:

Full datetime: 2026-04-22 14:35:10.123456
Date only: 2026-04-22
Time only: 14:35:10.123456

This can make your code clearer when you only need one part.

Formatting the result for display #

If you use print(now), Python uses the default datetime format.

If you want a custom format, use strftime().

Example: year-month-day #

from datetime import datetime

now = datetime.now()
print(now.strftime("%Y-%m-%d"))

Example output:

2026-04-22

Example: hour:minute:second #

from datetime import datetime

now = datetime.now()
print(now.strftime("%H:%M:%S"))

Example output:

14:35:10

This is useful when you want a cleaner, human-readable string.

To learn more, see datetime.strftime() explained. If you later need to turn a string back into a datetime value, see datetime.strptime() explained.

Timezone note for beginners #

datetime.now() gives local time by default.

For beginner use, that is often enough.

A few important points:

  • it uses the current local time on your system
  • basic use does not automatically include timezone information
  • two computers in different places may return different times

If you want to learn more about the bigger picture, start with the Python datetime module overview.

Common errors and confusion #

Here are some common mistakes beginners make with datetime.now().

Using the wrong import style #

This fails:

import datetime

print(datetime.now())

This works:

import datetime

print(datetime.datetime.now())

Or use:

from datetime import datetime

print(datetime.now())

Forgetting the parentheses #

This:

from datetime import datetime

print(datetime.now)

does not call the function. It gives you the method itself.

Use:

from datetime import datetime

print(datetime.now())

Expecting a string #

This returns a datetime object:

from datetime import datetime

now = datetime.now()
print(type(now))

If you need a string, use:

from datetime import datetime

now = datetime.now()
print(str(now))
print(now.strftime("%Y-%m-%d %H:%M:%S"))

Treating the module and class as the same thing #

This is very common:

  • datetime module
  • datetime class inside that module

That is why these two import styles behave differently:

from datetime import datetime

and

import datetime

Quick checks you can run #

If something is not working, try these commands:

python --version
python
from datetime import datetime
print(datetime.now())
import datetime; print(datetime.datetime.now())
type(datetime.now())

Common causes include:

  • imported the module with import datetime but called datetime.now() instead of datetime.datetime.now()
  • forgot parentheses and wrote datetime.now instead of datetime.now()
  • expected a string, but got a datetime object
  • tried to format the value without using strftime()
  • mixed up local time with timezone-aware time

FAQ #

What does datetime.now() return? #

It returns a datetime object with the current local date and time.

Why does datetime.now() not work after import datetime? #

Because datetime is then the module name. You need datetime.datetime.now().

How do I print only the date? #

Call .date() on the result, or use strftime() for a custom format.

How do I convert datetime.now() to a string? #

Use str() for the default format or strftime() for a custom format.

Is datetime.now() the same on every computer? #

No. It uses the current local time on that system.

See also #

Press Esc to close