In this tutorial, you’ll learn how to use Python to get the current date and time. Being able to work with current dates and times is an important skill in many different aspects in Python. For example, being able to get the current date and time can be helpful when logging information about your program.
By the end of this tutorial, you’ll learn:
- How to use Python to get today’s date
- How to format it to get different date parts,
- How to use Python to get the current time
- How to get the date and time in different timezones, UTC format, and in epoch time.
Let’s get started!
Table of Contents
The Quick: Get Current Date and Time
Below, you’ll find the quick answer to get the current date, time, and date/time in Python. Read on to learn all the details about how to do this!
# The Quick Answer
from datetime import datetime, date
today = date.today()
now = datetime.now().time()
current_datetime = datetime.now()
print(today)
print(now)
print(current_datetime)
# Returns:
# 2022-04-08
# 20:46:55.309192
# 2022-04-08 20:46:55.309198
How to Get Today’s Date in Python
Python comes with a library, datetime
, that handles dates and times in efficient manners. To start off, let’s take a look at how we can get today’s date using the datetime module.
# Use Python to Get Today's Date
from datetime import date
today = date.today()
print(today)
# Returns: 2022-04-08
Let’s break down what we did here:
- We imported the
date
module from the datetime library. As the name implies, this module handles strictly dates. - We then created a new variable,
today
, which is adate
object, set to today’s date.
What’s great about the fact that this returns a date
object. In the following two sections, you’ll learn how to use this object to apply methods and access attributes to gain more insight into the current date.
How to Format the Current Date in Python
In the previous section, you learned how to create a date
object that represents today’s date. The object was formatted as YYYY-MM-DD
. In this section, you’ll learn how to use the Python .strftime()
method to format the output of this date.
Let’s see how we can modify the output of this into a differently formatted string. The method accepts a specific format code that follows the 1989 C Implementation. For example, if we wanted to change the output to MM-DD-YYYY
format, we could write the following:
# Changing the Date Format with strftime()
from datetime import date
today = date.today()
print(today.strftime('%m-%d-%Y'))
# Returns: 04-08-2022
Similarly, if we wanted to spell out the date, we could write the following:
# Formatting today's date further
from datetime import date
today = date.today()
print(today.strftime('%B %d, %Y'))
# Returns: April 08, 2022
Below, you’ll find a number of important date time format codes you can use:
%a
: Weekday in the locale’s abbreviated name (e.g., Mon)%A
: Weekday in the locale’s full name (e.g., Monday)%b
: month as locale’s abbreviated name (e.g., Dec)%B
: month as locale’s full name (e.g., December)%m
: month as zero-padded decimal number (e.g., 01)%d
: day as a zero-padded decimal number (e.g., 09)%y
: year with century (e.g., 2021)%Y
: year without century (e.g., 21)
How to Get Current Date Parts in Python
There may be times when you only want to access a particular part of today’s date in Python.
Because the today()
function create a date
object, we can access attributes belonging to that object. The date
object has three attributes:
.day
returns the day as an integer.month
returns the month as an integer.year
returns the year as an integer
Let’s see what this looks like:
# Printing Date Parts Using Object Attributes
from datetime import date
today = date.today()
print(today.day)
print(today.month)
print(today.year)
# Returns
# 8
# 4
# 2022
Similarly, we can use the strftime()
method to return different date parts. For example, we can return the weekday or month name using this function. Let’s see what this looks like:
# Returning Date Parts Using strftime
from datetime import date
today = date.today()
print(today.strftime('%A'))
print(today.strftime('%B'))
# Returns:
# Friday
# April
In the following section, you’ll learn how to get the current time in Python.
How to Get the Current Time in Python
The datetime
module also makes it easy to get the current time using Python. In order to do this, we first have to import the datetime
module from the datetime
library.
Let’s see how we can use it to get the current time in Python:
# Get Current Time in Python
from datetime import datetime
current = datetime.now()
current_time = current.time()
print(current_time)
# Returns: 20:04:35.357504
Let’s break down what we did here:
- We imported the
datetime
module from thedatetime
library - We then created a new variable,
current
, which stores the current date and time - We then access only the time but using the
.time()
method
In the next section, you’ll learn how to access parts of the current time, such as hour, minute and second.
How to Get Current Time Parts in Python
There may be times when you’re interested in only seeing particular parts of the current time. In the previous section, after applying the .time()
method, we returned a time
object. This object comes with a number of useful attributes that describe the current time.
.hour
returns the hour of the current time.minute
returns the minute of the current time.second
returns the second of the current time.microsecond
returns the microsecond of the current time
Let’s take a look at what this looks like:
# Accessing Parts of the Current Time in Python
from datetime import datetime
current = datetime.now()
current_time = current.time()
print(current_time.hour)
print(current_time.minute)
print(current_time.second)
print(current_time.microsecond)
# Returns:
# 20
# 15
# 11
# 77321
How to Get the Current Date and Time in Python
In this section, you’ll learn how to use Python to get the current date and time. We can accomplish this using the datetime
module of the datetime
library. Similar to the example above, we can use the now()
function to access the current date and time. Let’s see what this looks like:
# Get the Current Date and Time in Python
from datetime import datetime
current = datetime.now()
print(current)
# Returns: 2022-04-08 20:19:46.304003
Because this returns a datetime object, you’re able to use the .strftime()
method to the object to format the datetime as a formatted string.
How to Get the Current Date and Time in UTC in Python
When building web applications or databases (or many other occasions), it’s likely that you’ll want to standardize your time to the UTC timezone. Thankfully, the datetime
module makes this very easy! We can simply pass in the UTC timezone into now()
function.
Let’s see what this looks like:
# Getting the Current Date and Time in UTC
from datetime import datetime, timezone
current = datetime.now(timezone.utc)
print(current)
# Returns: 2022-04-09 00:28:24.371627+00:00
In the example above, we also imported the timezone
module. This allowed us to pass in the utc
timezone as an argument into the now()
function. This, then, returned the current date and time in the UTC timezone.
How to Get the Current Date and Time in Another Timezone in Python
In order to get the current date and time using Python, we need to import the pytz
library as well as the datetime
module. This allows us to pass in a timezone based on, for example, the timezone name.
This works quite similar to how we accessed the current date and time in UTC. Let’s see how we can get the current date and time in Berlin:
# Getting the Current Date and Time in Another Timezone
from datetime import datetime
import pytz
current = datetime.now(pytz.timezone('Europe/Berlin'))
print(current)
# Returns: 2022-04-09 02:37:30.973091+02:00
To learn more about how to use timezones in Python, check out the official documentation here.
Conclusion
In this post, you learned how to use Python to get the current date, time, and datetime. You first learned how to get the current date, including accessing date parts and formatting the date in different ways. Then, you learned how to work with time objects and accessing time parts. Finally, you learned how to work datetime objects, as well as with different time zones.
Additional Resources
To learn more about related topics, check out the tutorials below: