If you ever have the need to print out and display the date and time in any of your Python projects, it turns out the code for executing those actions is very simple. To get started, you'll need to import the time module. From there, there are only a few short lines of code standing in the way of displaying the accurate and current date and time. Take a look at the code snippet below:
## 24 hour format ##
## 12 hour format ##
## dd/mm/yyyy format
## mm/dd/yyyy format
## dd/mm/yyyy hh:mm:ss format
print (time.strftime('%d/%m/%Y %H:%M:%S'))
## mm/dd/yyyy hh:mm:ss format
print (time.strftime('%m/%d/%Y %H:%M:%S'))
In the example above, you'll see 4 different options for the formats in which you can display your date and time. For time, there's the 24 hour format, which refers to a clock that includes all 24 hours, rather than a 12 hour clock that just repeats twice in a day, and there's also the option for a 12 hour format. There's an example that demonstrates how to display your date in the dd/mm/yyyy format, and one that demonstrates how to display the date in mm/dd/yyyy, which is the date format that's most popular in the US.
Finally, there's an example that combines both date and time in the day/month/year, hour:min:sec format (and an example with the month coming before the day, for coders in the US). Feel free to add these to any of your Python projects where you want to easily and quickly display the date and time.To Practice: Try this interactive course on the basics of Lists, Functions, Packages and NumPy in Python.