Convert timestamps with offset to datetime obj using strptime

Convert timestamps with offset to datetime obj using strptime

This blog will explain how to convert the timestamps with offset to datetime object using strptime() function.

Converting Time Strings to Datetime

To parse a datetime string having the timezone offset in Python, the strptime() Function can be used. The strptime() Function in DateTime, is utilized to format the time stamp which is in string format to date-time object.

How strptime() works?

The strptime() Function accepts 2 arguments, ie the time_data and the format_data, which are parsed using the specified format, that will generate the datetime object that represents the extracted date and time. The generated object can further be utilized for other manipulations such as formatting, calculations, or timezone conversions.

Syntax

datetime.strptime(time_data, format_data)

Parameters

  • time_data:  It represents the time present in string format.
  • Format_data: This parameter represents the data present in datetime format which is converted from time_data using this function.

Return

This function returns the datetime object of the given date_string.

Following is the list of acceptable formats that are passed to strptime() Function:

Format SpecifierDescriptionOutput
%YYear in four digits2025
%mMonth as a zero-padded decimal number01,02, 03, 04,…11,12
%dDay of the month as a zero-padded decimal number.01, 02, …, 30, 31
%HHour (24-hour clock) as a zero-padded decimal number.00, 01, …, 23
%MMinute as a zero-padded decimal number.00, 01, …, 59
%SSecond as a zero-padded decimal number.00, 01, …, 59
%zUTC offset in the form ±HHMM+0000, -0400, +0530

Example

# Python3 code to get the current date-time stamp

# importing datetime module
from datetime import datetime

# using now() to get the current date-time
current_datetime = datetime.now()

# Printing current datetime
print("Current Date-Time Stamp:", current_datetime)

Output

Current Date-Time Stamp: 2025-01-28 14:35:45.123456

Here, the datetime.now() function will fetch the current local date and time as a datetime object including the microseconds.

Conclusion

In Python, the strptime() function is an essential tool to parse the date-time strings into datetime objects, including the timezone offsets. For interpreting and retaining the timezone information, the %z format specifier can be utilized which enables accurate time-based computations and conversions.

By utilizing the strptime(), developers can convert and manipulate datetime strings, making it easier to work with time-sensitive data in Python efficiently.

FAQs

How to use the strptime() function in Python?

strptime() is a function in Python’s datetime module that parses a date-time string into a datetime object based on a specified format.

How to use strptime() parsing a datetime string with a timezone offset?

Include the %z format specifier to correctly interpret timezone offsets.

What format specifier need to use for timezone offsets?

Use %z to parse timezone offsets like +05:30 or -08:00.

What happens if the datetime string does not have a timezone offset?

If the string lacks a timezone and having the %z in the format, the strptime() will raise a ValueError, which will ensure that the format matches the input string exactly.

Is it possible to use the strptime() to handle the UTC or named time zones (e.g., EST, PST)?

No, the strptime() supports numeric timezone offsets (e.g., +0530) only. Handling the named time zones can be achieved by using the libraries like pytz or zoneinfo.

How to convert a parsed datetime object to a different timezone?

Use astimezone() after parsing the datetime object

About the Author

Data Engineer

As a skilled Data Engineer, Sahil excels in SQL, NoSQL databases, Business Intelligence, and database management. He has contributed immensely to projects at companies like Bajaj and Tata. With a strong expertise in data engineering, he has architected numerous solutions for data pipelines, analytics, and software integration, driving insights and innovation.

business intelligence professional