The datetime.now() function from the datetime module is used to get the current date and time up to the microsecond in Python.
Example 1: Get Current Date and Time
Visual Representation
from datetime import datetime
# Get the current date and time based on the local system's setting
current_datetime = datetime.now()
print(current_datetime)
Output
2024-01-31 10:51:05.345968
The strftime method allows you to format datetime object into readable string.
Visual Representation
# Format the date and time
formatted_date = current_datetime.strftime("%Y-%m-%d %H:%M:%S")
print(formatted_date)
Output
2024-01-31 10:51:05
Example 2: Get only current(today’s) date
The date.today() is used to get the current local date.
Visual Representation
from datetime import date
# Retrieve the current date
current_date = date.today()
print(current_date)
Output
2024-01-31
We can also format the date according to user requirements.
from datetime import date
# Get the current date
current_date = date.today()
# Format: YYYY-MM-DD
format1 = current_date.strftime("%Y-%m-%d")
# Format: DD/MM/YYYY
format2 = current_date.strftime("%d/%m/%Y")
# Format: DD-Mon-YYYY
format3 = current_date.strftime("%d-%b-%Y")
# Format: Month DD, YYYY
format4 = current_date.strftime("%B %d, %Y")
# Format: Day, Month DD, YYYY
format5 = current_date.strftime("%A, %B %d, %Y")
# Print the dates in different formats
print("Format 1:", format1)
print("Format 2:", format2)
print("Format 3:", format3)
print("Format 4:", format4)
print("Format 5:", format5)
# %Y represents the year
# %m represents the month
# %d represents the day of the month
# %b represents the abbreviated month name.
# %B represents the full month name.
# %A represents the full weekday name.
Output
Format 1: 2024-01-31
Format 2: 31/01/2024
Format 3: 31-Jan-2024
Format 4: January 31, 2024
Format 5: Wednesday, January 31, 2024
Example 3: Get only current time
The .time() method extracts just the time part from the date and time.
Visual Representation
from datetime import datetime
# Retrieve the current time
current_time = datetime.now().time()
print(current_time)
Output
10:51:54.169874
Example 4: Get the current time in UTC
If you are building applications and websites for a global audience, using UTC(Coordinated Universal Time) as a base makes it easier to manage and present date and time information to users around the world in their respective local times.
Keep in mind that there is no time difference between GMT(Greenwich Mean Time) and UTC.
Visual Representation
from datetime import datetime
current_utc_time = datetime.utcnow()
print("Current UTC time:", current_utc_time)
Output
Current UTC time: 2024-01-31 06:11:07.505488
Example 5: Get a Particular Timezone
If you want to work with a particular timezone, you can use the pytz library, which allows accurate and cross-platform timezone calculations.
First, you need to install pytz if you haven’t already:
pip install pytz
or
python3 -m pip install pytz
from datetime import datetime
import pytz
# Timezones
timezones = ['America/New_York', 'Europe/London', 'Asia/Kolkata',
'Australia/Sydney', 'Africa/Johannesburg']
# Get current time in each timezone
for tz in timezones:
timezone = pytz.timezone(tz)
current_time = datetime.now(timezone)
print(f"Current time in {tz}: {current_time}")
Output
Current time in America/New_York: 2024-01-31 00:41:55.735476-05:00
Current time in Europe/London: 2024-01-31 05:41:55.736219+00:00
Current time in Asia/Kolkata: 2024-01-31 11:11:55.736501+05:30
Current time in Australia/Sydney: 2024-01-31 16:41:55.736848+11:00
Current time in Africa/Johannesburg: 2024-01-31 07:41:55.737056+02:00