Working with dates and times is one of the biggest challenges in programming. Between dealing with time zones, daylight saving time, and different written date formats, it can be tough to keep track of which days and times you’re referencing. Fortunately, the built-in Python datetime module can help you manage the complex nature of dates and times.
In this tutorial, you’ll learn:
- Why programming with dates and times is such a challenge
- Which functions are available in the Python datetime module
- How to print or read a date and time in a specific format
- How to do arithmetic with dates and times
The Python time module provides many ways of representing time in code, such as objects, numbers, and strings. It also provides functionality other than representing time, like waiting during code execution and measuring the efficiency of your code.
This article will walk you through the most commonly used functions and objects in time.
By the end of this article, you’ll be able to:
- Understand core concepts at the heart of working with dates and times, such as epochs, time zones, and daylight savings time
- Represent time in code using floats, tuples, and struct_time
- Convert between different time representations
- Suspend thread execution
- Measure code performance using perf_counter()
These are online read-only versions. However you can Run ▶
all the codes online by clicking here ➞
You can and Starring and Forking is free for you, but it tells me and other people that it was helpful and you like this tutorial.
Go here
if you aren't here already and click ➞ ✰ Star
and ⵖ Fork
button in the top right corner. You will be asked to create a GitHub account if you don't already have one.
-
Go
here
and click the big green ➞Code
button in the top right of the page, then click ➞Download ZIP
. -
Extract the ZIP and open it. Unfortunately I don't have any more specific instructions because how exactly this is done depends on which operating system you run.
-
Launch ipython notebook from the folder which contains the notebooks. Open each one of them
Kernel > Restart & Clear Output
This will clear all the outputs and now you can understand each statement and learn interactively.
If you have git and you know how to use it, you can also clone the repository instead of downloading a zip and extracting it. An advantage with doing it this way is that you don't need to download the whole tutorial again to get the latest version of it, all you need to do is to pull with git and run ipython notebook again.
I'm Dr. Milaan Parmar and I have written this tutorial. If you think you can add/correct/edit and enhance this tutorial you are most welcome🙏
See github's contributors page for details.
If you have trouble with this tutorial please tell me about it by Create an issue on GitHub. and I'll make this tutorial better. This is probably the best choice if you had trouble following the tutorial, and something in it should be explained better. You will be asked to create a GitHub account if you don't already have one.
If you like this tutorial, please give it a ⭐ star.
You may use this tutorial freely at your own risk. See LICENSE.