w3resource

Python: Get the first and last second


52. First and Last Second

Write a Python program to get the first and last second.

Sample Solution:

Python Code:

# Import the datetime module
import datetime

# Print the minimum time (midnight) in the datetime module
print("First Second: ", datetime.time.min)
# Print the maximum time (11:59:59.999999) in the datetime module
print("Last Second: ", datetime.time.max)

Output:

First Second:  00:00:00                                                                                       
Last Second:  23:59:59.999999 

Explanation:

In the exercise above,

  • The code imports the "datetime" module, which provides classes for manipulating dates and times.
  • It prints the minimum time available in the datetime module using "datetime.time.min".
  • It prints the maximum time available in the datetime module using "datetime.time.max".

Flowchart:

Flowchart: Get the first and last second.

For more Practice: Solve these Related Problems:

  • Write a Python program to compute the first and last second of the current day and display both in "HH:MM:SS" format.
  • Write a Python script to extract the first second (00:00:00) and last second (23:59:59) of a given date and then print them.
  • Write a Python function to determine the starting and ending timestamps of any given day and then format them as datetime strings.
  • Write a Python program to calculate the first and last second of a user-specified date and then compare these times with the current time.

Go to:


Previous: Write a Python program to generate RFC 3339 timestamp.
Next: Write a Python program to validate a Gregorian date. The month is between 1 and 12 inclusive, the day is within the allowed number of days for the given month. Leap year’s are taken into consideration. The year is between 1 and 32767 inclusive.

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.