Photo by Fabian Albert on Unsplash. This article is originally published on dev.to on 10 Apr 2021.

When dealing with data, we often find ourselves handling date and time. Although it is not particularly difficult, I often find myself googling the same conversion over and over again.

In this post, I'll share a cheat sheet of date and time conversion to eliminate such little time-consuming procedures and explain each technique in the following sections.

Open in PDF

Python date & time conversion CheatSheet

Table of Contents

1️⃣ String to DateTime object

2️⃣ DateTime object to String

3️⃣ String to String

4️⃣ UTC timestamp in seconds to DateTime object

5️⃣ UTC timestamp in seconds to String

6️⃣ UTC time string to UTC DateTime object in different GMT

7️⃣24-hour time String to 12-hour time String

8️⃣Download PDF file

String to DateTime object

Input Example: String "08/04/2021" Output: Datetime object "2021-04-08 00:00:00"

We can use datetime.strptime to create a DateTime object from a string. strptime method takes two arguments -- target string and string to explain the format of the target string.

String to DateTime object

DateTime object to String

Input Example: DateTime object "2021-04-08 00:00:00" Output: String "08/04/2021"

We can use datetime.strftimeto convert a DateTime object into String. This strftime method takes two arguments -- DateTime object and String to specify desired string format.

DateTime object to String

String to String

Input Example: String "08/04/2021" Output: String "2021-04-08"

To convert datetime string into a different format of a string, we first need to convert it into a DateTime object. Then we can use strptimeto specify and convert into the desired format.

String to String

UTC timestamp in seconds to DateTime object

Input Example: float 1617836400.0 (UTC timestamp in seconds) Output: DateTime object with "2021-04-08 00:00:00"

To convert float of timestamp into a DateTime, can use datetime.fromtimestamp(). It takes timestamp as an argument and returns DateTime object.

timestamp to DateTime object

UTC timestamp in seconds to String

Input Example: float 1617836400.0 (UTC timestamp in seconds) Output: String "08/04/2021"

To convert float of timestamp into String, we first get a DateTime object. Using strftime method, we can then create desired format of String.

timestamp to String

UTC time string to UTC DateTime object in different GMT

Input Example: String "08-04-2021 08:33:00-0400" (New York timezone) Output: DateTime object "2021-04-08 21:33:00+09:00" (Tokyo timezone)

To convert an UTC time string, we first convert it into a DateTime object. After that, we can use astimezone method to create a DateTime object in specified GMT.

UTC String to different UTC DateTime object

24-hour time String to 12-hour time String

Input Example: String "20:00" Output: String "08:00 PM"

We first need to convert String into a DateTime object. Right after, 12-hour time string can be created using strftime method.

24-hour time String to 12-hour time String

Download PDF file

Open PDF


Thanks for reading!

READ NEXT


3 Main Benefits of Feasibility Study

Software Development

C# date & time conversion CheatSheet

C#

Examples to remember the concept of Abstraction, Generalisation, Decomposition and Encapsulation in one go

Software Development

Started Software Development Apprenticeship programme in FIT Ireland!

Software Development