w3resource

Python: Display the date and time in a human-friendly string

Python Datetime: Exercise-34 with Solution

Write a Python program to display the date and time in a human-friendly string.

Sample Solution:

Python Code:

# Import the time module
import time
# Print an empty line
print()
# Print the current time in a human-readable format
print(time.ctime())
# Print an empty line
print()

Output:

Mon May  8 13:04:35 2017 

Explanation:

In the exercise above,

  • The code imports the "time" module, which provides various functions for working with time-related tasks in Python.
  • It prints an empty line using the "print()" function. This is just for formatting purposes to add a blank line before the next output.
  • It prints the current time using the "ctime()" function from the "time" module. This function returns a string representing the current local time in a human-readable format.
  • Next it prints another empty line using the "print()" function.

Flowchart:

Flowchart: Display the date and time in a human-friendly string.

Python Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Python program to calculate number of days between two datetimes.
Next: Write a Python program to convert a date to Unix timestamp.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/python-exercises/date-time-exercise/python-date-time-exercise-34.php