w3resource

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


34. Human Friendly DateTime

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.

For more Practice: Solve these Related Problems:

  • Write a Python program to display the current date and time in a human-friendly format like "Tuesday, April 13, 2021 at 12:02 PM".
  • Write a Python script that formats a datetime object into a natural language string describing the day, date, and time.
  • Write a Python function to convert a datetime object into a friendly string that includes the ordinal day (e.g., "13th") and month name.
  • Write a Python program to output a datetime in a conversational format, such as "It is currently 12:02 on Tuesday, April 13, 2021".

Go to:


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.

Python Code Editor:

Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.