Python date & time conversion CheatSheet
Maiko Miyazaki
Posted on April 10, 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.
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
7️⃣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.
DateTime object to String
Input Example: DateTime object "2021-04-08 00:00:00"
Output: String "08/04/2021"
We can use datetime.strftime
to convert a DateTime object into String. This strftime
method takes two arguments -- DateTime object and String to specify desired string format.
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 strptime
to specify and convert into the desired format.
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.
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.
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.
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.
Download PDF file
Thanks for reading. If you have any opinion or question, please leave a comment below! In the meantime, please follow me on Linkedin @Maiko Miyazaki or Twitter @Maiko Miyazaki
Posted on April 10, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.