6

Python date & time conversion CheatSheet

 3 years ago
source link: https://dev.to/maikomiyazaki/python-date-time-conversion-cheatsheet-3m69
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Cover image for Python date & time conversion CheatSheet

Python date & time conversion CheatSheet

Apr 10

・3 min read

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

Table of Contents

1️⃣ String to DateTime object
2️⃣ DateTime object to String
3️⃣ String to String
4️⃣ timestamp to DateTime object
5️⃣ timestamp 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.strftimeto 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 strptimeto specify and convert into the desired format.

timestamp to DateTime object

Input Example: timestamp 1617836400.0
Output: DateTime object with "2021-04-08 00:00:00"

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

timestamp to String

Input Example: timestamp 1617836400.0
Output: String "08/04/2021"

To convert 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

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK