Time

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.

import time
print (time.time())

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.

import time
print (time.strftime("%H:%M:%S"))

You can use the same method to get the date.

import time
print (time.strftime("%d/%m/%Y"))

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

# %a	Weekday name.
print(time.strftime("%a"))

# %A	Full weekday name.
print(time.strftime("%A"))

# %b	Abbreviated month name.
print(time.strftime("%b"))

# %B	Full month name.
print(time.strftime("%B"))

# %c	Appropriate date and time representation.
print(time.strftime("%c"))

# %d	Day of the month as a decimal number [01,31].
print(time.strftime("%d"))

# %H	Hour (24-hour clock) as a decimal number [00,23].
print(time.strftime("%H"))

# %I	Hour (12-hour clock) as a decimal number [01,12].
print(time.strftime("%I"))

# %j	Day of the year as a decimal number [001,366].
print(time.strftime("%j"))

# %m	Month as a decimal number [01,12].
print(time.strftime("%m"))

# %M	Minute as a decimal number [00,59].
print(time.strftime("%M"))

# %p	Equivalent of either AM or PM.
print(time.strftime("%p"))

# %S	Second as a decimal number [00,61].
print(time.strftime("%S"))

# %U	Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.
print(time.strftime("%U"))

# %w	Weekday as a decimal number [0(Sunday),6].
print(time.strftime("%w"))

# %W	Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.
print(time.strftime("%m"))

# %x	Appropriate date representation.
print(time.strftime("%x"))

# %X	Apropriate time representation.
print(time.strftime("%X"))

# %y	Year without century as a decimal number [00,99].
print(time.strftime("%y"))

# %Y	Year with century as a decimal number.
print(time.strftime("%Y"))

# %Z	Time zone name (no characters if no time zone exists).
print(time.strftime("%Z"))

# %%	A literal '%' character.
print(time.strftime("%%"))

 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.