TNS
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Programming Languages / Python / Software Development

How To Work With Date and Time in Python

This tutorial shows Python developers how to easily access the system clock using the `datetime` module.
Oct 10th, 2024 7:14am by
Featued image for: How To Work With Date and Time in Python
Featured image via Unsplash.

We expect our applications and services to always be on time. Tasks like automation, data collection, scheduling, security and IoT integrations would look completely different without the confidence of precise timing. The world would look completely different if each developer built their applications and functions based on their watch. Fortunately, we have the system clock, which provides a universal reference across all programming languages and hardware. In Python, you can easily access this clock using the datetime module.

The datetime module references the system clock. The system clock is a hardware component in computers that tracks the current time. It counts the seconds since a fixed point known as the “epoch,” which is Jan. 1, 1970, on most systems.

Operating systems provide an interface for applications to access the system clock through system calls or APIs. These system calls and APIs return the current date and time. The accuracy and precision of this time depend on the hardware and the OS’s timekeeping mechanisms, but it all starts from the same place.

Python’s time interface is the datetime module. It calls system APIs to retrieve the current date and time.

How Does datetime Work?

To first work with dates and times, you’ll need to import the datetime module. The module will import all the methods and attributes of the datetime object into your application. Working with the datetime object will follow the object-oriented programming syntax.

To get the current date and time, you can use the datetime.now() method. It will return the full datetime object with the current date and time down to the nanosecond.

The format is: 2024-07-30 08:59:46.989846

You can also split this if you only need the date or only need the time. Calling the following two methods will extract more limited information from the datetime object.

To print today’s date, use the date.today() method:

To pull just the current time for your application, you’ll have to extract the time from the datetime object.

Formatting

You can reformat the dates and times as strings using the strftime() method. This allows you to specify your preferred format using format codes. Here’s a common format code:

%Y updates the year

The following codes update the specified time as a zero-padded decimal number (for example, 01):

%m updates the month
%d updates the day
%H updates the 24-hour clock
%M updates the minute
%S updates the second

A complete block of code that utilizes these format codes might look like this:

Working With Time Zones

You can adjust the datetime object to reflect different time zones using the pytz library. Before you use it, you’ll need to import it:

It’s not required that you get the UTC time first, but it is best practice because the UTC never changes (including during daylight savings time), so it’s a strong reference point.

Python’s datetime module saves the date!

The datetime module simplifies working with timing in Python. It eliminates much of the complexity involved in synchronizing applications and ensures they operate with accurate, consistent timing.

Group Created with Sketch.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.