How To Get the Current Date in Python

In this quick tutorial, we’ll show you how to get the current date and time in Python. We’ll be using the datetime module from the standard library.

Checkout this video:

Introduction

Python provides a number of ways to get the current date and time. Most certainly, you can use the built-in function `datetime`:

The datetime Module

The datetime module in Python is used to work with dates and times. It provides various functions and classes to work with dates and times.

To get the current date and time, you can use the datetime.now() function. This function returns a datetime object that contains the current date and time.

You can also use the datetime.date() function to get only the date component of the current date. This function returns a date object that contains only the date component of the current date.

If you want to get only the time component of the current time, you can use the datetime.time() function. This function returns a time object that contains only the time component of the current time.

The time Module

The time module provides many time-related functions. Supplemental functions are also available to assist in formatting dates and times, and working with time zones.

import time

##Returns the current system time in seconds since the Epoch (January 1st, 1970 at Midnight)
currentTime = time.time()
print(“Current Time:”, currentTime)

##Returns the current local date and time, as a string. The format of the string will depend on your system’s settings.
currentDateTime = time.strftime(“%Y-%m-%d %H:%M:%S”)
print(“Current Date & Time:”, currentDateTime)

##Returns the local date and time, as a tuple. The elements of the tuple will depend on your system’s settings. For example, on my computer, it returns:
##(2019, 11, 5, 16, 2, 21, 1, 309, 0)
currentLocalDateTime = time.localtime()
print(“Current Local Date & Time Tuple:”, currentLocalDateTime)

The calendar Module

The calendar module is useful for working with dates. It can be used to determine the day of the week for a given date, calculate the number of days in a month, or create a text calendar.

The calendar module provides several functions that are useful for working with dates. The most important one is the calendar() function, which returns a list of lists containing information about a month’s calendar.

The first list in the return value from calendar() is a list of strings that represent the days of the week. The first element of this list is Sunday, followed by Monday, Tuesday, and so on. The second element is a list of tuples containing (day_of_month, day_of_week), where day_of_week is an integer representing the day of the week (0=Sunday, 1=Monday, and so on).

Here’s an example of how to use calendar():
“`python
import calendar
c = calendar.calendar(2020)
print(c)
“`

Formatting Dates and Times

It is often useful to format dates and times in Python in a way that is easy for humans to read. For example, you might want to display the current date and time in a activity log. Python provides a number of ways to format dates and times. In this article we will look at the two most common ways.

The first way is to use the strftime() function. This function take a format string as its first argument and a date or time object as its second argument. The format string contains directives that tell strftime() how to format the date or time. For example, %d will format the day of the month as a two-digit number, %b will format the month as an abbreviation, %Y will format the year as a four-digit number, etc. A complete list of directives can be found in the Python documentation.

Here is an example of how to use strftime():

>>> import datetime
>>> now = datetime.datetime.now()
>>> now
datetime.datetime(2017, 1, 27, 16, 29, 3, 894290)
>>> now.strftime(“%b %d %Y %H:%M”)
‘Jan 27 2017 16:29’

The second way to format dates and times is to use the strptime() function. This function acts like the inverse of strftime(). It takes a date or time string as its first argument and a format string as its second argument. The strptime() function then tries to parse the date or time string according to the directives in the format string. For example, if we have a date string that looks like this:

“01/27/2017 16:29”

We can parse it into a datetime object like this:

>>> from datetime import datetime
>>> dt_string = “01/27/2017 16:29”
>>> dt = datetime.strptime(dt_string, “%m/%d/%Y %H:%M”)
>>> dt
datetime(2017, 1, 27, 16, 29)

Parsing Dates and Times

There are a number of ways to get the current date and time in Python. Perhaps the most straightforward way to do this is with the built-in datetime module. This module provides a number of classes, including adate, which represents a single calendar date. The following code snippet shows how to create an instance of the datetime.date class and print out the current date:

import datetime

today = datetime.date.today()
print(today)
# Output: 2016-06-06
You can also use the now() method to create an instance of the datetime.datetime class, which stores both date and time information:

import datetime

now = datetime.datetime.now()
print(now)
# Output: 2016-06-06 13:15:02.476094

Working with Time Zones

There are a couple different ways to get the current date in Python. The most common way is to use the standard library module datetime. This module comes with a built-in function called datetime.now() that returns the current date and time in UTC format.

If you want to work with a specific time zone, you can import the pytz module and use one of its time zone objects. For example, to get the current date and time in Los Angeles, you would do the following:

import pytz
la_time = pytz.timezone(‘America/Los_Angeles’)
datetime.now(la_time)

This will return a datetime object with the current date and time in Los Angeles (UTC-8).

Measuring Elapsed Time

There are many ways to measure elapsed time in Python. Here are some of the most popular methods:

The time module provides a function, also called time, that returns the number of seconds since the Unix epoch (January 1, 1970 at 00:00:00 GMT).

You can also use the timeit module to measure the elapsed time of a small code snippet. The timeit module provides a Timer class that you can use to time the execution of a code snippet.

Handling Leap Years

There are a few things we need to consider when handling dates, especially when it comes to months with different numbers of days, or “leap” years.

Leap years are years where an extra day, February 29th, is added to the calendar. We account for leap years by checking if the year is evenly divisible by 4. If it is, we check if it’s evenly divisible by 100. If it is, we check if it’s evenly divisible by 400.

If the year is evenly divisible by 4 and not 100, or is evenly divisible by 400, then it’s a leap year and February 29th is added. Otherwise, it’s not a leap year and February 28th is used.

Conclusion

In Python, getting the current date is extremely easy. All you need to do is import the datetime module. Once you have done that, you can use the datetime.now() function to get the current date and time.

Scroll to Top