Learn more than the basics about the date, time, and timezone in Python

Let’s become a time master in Python

Lynn G. Kwong
5 min readAug 13, 2021

All Python developers should have some knowledge about how to deal with the date and time in Python. I assume everybody has already used time.sleep() and datetime.now() in their code. However, sometimes you might come across special cases which are beyond your knowledge and you have to turn to Google every time to find the best solution for you, which can be a time-consuming process sometimes. This article goes through some of the most frequently used tricks about the date, time, and timezone in Python in our daily work, which shall help you solve different levels of time-related issues in Python.

Picture by Igor Son on Unsplash.

Let’s start from the very basics. We can use the familiar datetime module to get the current date and/or time.

now and today are Python datetime and date objects, respectively. If you need to add type hints for the datetime and date objects, you can import the datetime and date types from the datetime module and use them for typing. As the datetime and date

--

--

Lynn G. Kwong

I’m a Software Developer (https://medium.com/@lynn-kwong) keen on sharing thoughts, tutorials, and solutions for the best practice of software development.