It is not uncommon to use time (or dates) or programs in some way or another.   If you are writing games you might want to record how long something takes or give the player a restricted amount of time.  Thankfully Python has some pretty good libraries (datetime, time) to help you.

Current Time

The Python time.time() method is probably the most useful method for timing and timers as it returns the number of milliseconds expired since the epoch.

Current Date and/or Time as a formated string

If you wanted to know the current date you can use the time library and its strftime method. Change the H to an I and the time will be returned as 12 hour format rather than 24 hour format.

You can use the same method to get the date.

The following directives can be embedded in the format string (click here to see the output):

 Python Time Difference

In this  code example I show how it is possible to get the time between a start and end time.

pythonTimeDiff.jpg - 11/2015

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.