DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

Cover image for Python date & time conversion CheatSheet
Maiko Miyazaki
Maiko Miyazaki

Posted on • Updated on

Python date & time conversion CheatSheet

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
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.

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. 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

Top comments (4)

Collapse
shadowlion profile image
Justin Chiou

Really nice and clean! I would say the one thing to note for newbies who are using this - timestamp values are based on the number of seconds since epoch (January 01, 1970 UTC). I know it says the type is of timestamp, but one can forget to what order we base things off of - regardless, well done!

Collapse
maikomiyazaki profile image
Maiko Miyazaki Author

Thank you so much for pointing out Justin :) With your comment, I realized that I was not fully aware of the timestamp! As a result, I've managed to improve the article and PDF to clarify the values and types of the example. Thank you for taking your time to share it here! ๐Ÿ˜Š๐Ÿ™

Collapse
aalphaindia profile image
Pawan Pawar

Good one!!

Collapse
maikomiyazaki profile image
Maiko Miyazaki Author

Thank you Pawan!

๐Ÿค” Did you know?

ย 
โœ๏ธ Writing your own article is easy (we even support markdown).