w3resource

Python: Get the system time

Python Basic: Exercise-98 with Solution

Write a Python program to get the system time.

Note : The system time is important for debugging, network information, random number seeds, or something as simple as program performance.

Pictorial Presentation:

Get the system time

Sample Solution-1:

Python Code:

import time
print()
print(time.ctime())
print()

Sample Output:

Thu Apr 20 18:25:06 2017

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:


Sample Solution-2:

Python Code:

import datetime
print(datetime.datetime.now())

Sample Output:

2021-06-04 08:40:11.391443

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:


Python Code Editor:

 

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to list the special variables used within the language.
Next: Write a Python program to clear the screen or terminal.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Try-catch-else construct:

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")