Free cookie consent management tool by TermsFeed The now() method of datetime class in Python | Pythontic.com

The now() method of datetime class in Python

Overview:

  • The function now() returns current local time and date. Without specifying any timezone instance – the datetime returned is not timezone aware.

  • If a concrete timezone instance is passed to the tz parameter then the value is offset with the current local time and date and the returned value is the time in specified timezone.

  • The now() function returns a datetime object.

  • The fold attribute of the returned datetime object can not be modified regardless of the timedelta of the timezone object passed to the now() function. For any such modification a new datetime instance has to be created.

  • The DateTime objects are not DST aware. DST manipulations can be done through derived classes or through other means. However when such DST code is maintained the fold attribute of the datetime object can be set accordingly.

Example:

# Example Python program that gets the current time
# through the Python standard library function 
# datetime.now()
import datetime as dt

# Get the current time - without any timezone 
currentTime = dt.datetime.now()
print("Current local time:")
print(currentTime)
print("Timezone:")
print(currentTime.tzinfo)

# Get the current time in New York City
# Assuming DST is not active. 
utcOffset     = dt.timedelta(hours = -5)
est         = dt.timezone(utcOffset)
currentTime = dt.datetime.now(tz = est)
print("EST (No DST):")
print(currentTime)
print("DST set?")
print(currentTime.fold)
print("Timezone:")
print(currentTime.tzinfo)

# UTC during no DST
currentTimeUTC = dt.datetime.now(tz = dt.timezone.utc)
print("UTC now(No DST):")
print(currentTimeUTC)
print("Timezone:")
print(currentTimeUTC.tzinfo)

Output:

Current local time:
2025-07-29 12:34:26.391358
Timezone:
None
EST (No DST):
2025-07-29 02:04:26.391387-05:00
DST set?
0
Timezone:
UTC-05:00
UTC now(No DST):
2025-07-29 07:04:26.391525+00:00
Timezone:
UTC

 


Copyright 2025 © pythontic.com