Learn to Code via Tutorials on Repl.it!

← Back to all posts
Time and DateTime in Python


I have doubt that most of you have used the time and datetime modules in Python. However, I also would also say that most of you don't know its full capabilities either. If you have used the time module, then chances are that it was to get a time, date or maybe even test the time it task your functions to execute.


As mentioned before, we will be covering the time and datetime modules. However, to go with these, there is one function from the calendar module we will be using. They are all in the standard library. These modules allow us to get times and dates based on epoch, utc and local times.

  • Epoch is the time elapsed since 1st January 1970 00:00:00.
  • UTC (Coordinated Universal Time) is within ~1 second of mean solar time. Your country may use GMT (Greenwich Mean Time) instead.
  • Local time is the time reckoned for your region.

When we get these times, they will be in one of these data types.

  • struct_time which is a tuple containing the data for the time.
  • str which will be the time and possibly date.
  • float which will most likely be a time in seconds.
  • int whcih will most likely be a time in nano seconds.

Note: What you see below is going to be a lot of code, I mean, a lot. But it comes with explanations like always.


To get started with time, we need to do some importing:

Now we can begin!

Some of the commonly used functions consist of:

You may have used them to add a dramatic flair to your game or to get the weather at a specific time. But there are so many other things that are in the time module.

Now, lets move onto some of the more advanced things that we talked about above:

The first two functions will get the utc and local time from the epoch, whereas, the second two functions get the epoch from the utc and local times.

In this case, we are making strings out of the times we got from before. This makes it much easier if we just want to display the date and not get anything specific from it.

This will get the value of a monothic click. These asre useful as they don't go back or leap forward. They are one steady clock which goes up by one each time.

This is another clock, but unlike the others, it has a high resolution. This is great to use if you are measuring small changes in time, or need a a highly accurate value for your program.

This returns the runtime of the program. It does this based on the system and CPU times. This can become very useful to check if some time has ellapsed in your program.

Notice that the functions which return float values (which are most likely seconds), also have another function with _ns at the end. This will return that functions output but as an int, which will be that value but in nano seconds.


Fistly, we need to import our modules:

Let's cover some of the basic functionality that comes with the datetime module.

These will return datetime classes which equal the current date or the timestamps date. Here the timestamp is time.time(), but this could be a timestamp that you saved earlier.

These are simple classes which allow you to easier represent the time, date or both at the same time. For date and datetime, the year, month and day should be integers and are required. time takes in hour, minute, second and microsecond as integers, but they will default to 0 if not given.

timedeltas can prove very useful when comparing times. Having the ability to compare them using our normal operators (+, -, *, /...) makes it much more convenient as well.

This allows us to convert the datetime into something that might be useful in other programs. It is also a smaller value to save and/or use.

Again, this might come in useful uif you only need a bit of data from the datetime, or want to get it in a specific style.

Note: These functions, for the most part, also apply to the time and date classes.


There are a lot more to these classes then most people thing, even more than what I've shown here. It's always best to play around with the docs to see what you can find use in.

This tutorial was suggested by @TristanMisja .
If you have any suggestions for tutorials, leave them in the comments and I'll be sure to have a look. If you like one in the comments, then give it an up vote to show that you want to see it. It makes my life so much more easier. Thanks in advance!


Wow, thanks!


@TristanMisja No problemo!


so how would I get the current second?