Python program to print current hour, minute, second and microsecond
Last Updated :
16 May, 2023
In this article, we are going to discuss how to print current hour, minute, second, and microsecond using Python. In order to print hour, minute and microseconds we need to use DateTime module in Python.
Methods used
- datetime.now().hour(): This method returns the current hour value of the datetime object.
- datetime.now().minute(): This method returns the current minute value of the datetime object.
- datetime.now().second(): This method returns the current second value of the datetime object.
- datetime.now().microsecond(): This method returns the current microsecond value of the datetime object.
Below are the various implementations using examples that depict how to print current hour, minute, second, and microsecond in python.
Example 1: To print time, hour, minute, second, and microsecond
Python3
from datetime import datetime
myobj = datetime.now()
print ( "Current hour " , myobj.hour)
print ( "Current minute " , myobj.minute)
print ( "Current second " , myobj.second)
print ( "Current microsecond " , myobj.microsecond)
|
OutputCurrent hour 4
Current minute 5
Current second 13
Current microsecond 121302
Time complexity: O(1) – constant time complexity, since the code is only retrieving and printing the current time values.
Auxiliary space: O(1) – constant space complexity, since the code is not using any additional data structures or variables that increase with input size.
Example 2: To print object, time, hour, minute, second, and microsecond.
Python3
from datetime import datetime
myobj = datetime.now()
print ( "Object:" , myobj)
print ( "Current hour " , myobj.hour)
print ( "Current minute " , myobj.minute)
print ( "Current second " , myobj.second)
print ( "Current microsecond " , myobj.microsecond)
|
OutputObject: 2022-11-24 04:07:13.707730
Current hour 4
Current minute 7
Current second 13
Current microsecond 707730
Time Complexity: O(1)
Auxiliary Space: O(1)
Approach#3: Using time module
We can use the built-in time module in Python to get the current time. We can use the time() function of the time module to get the number of seconds and then extract the hour, minute, second, and microsecond components from it.
Algorithm
1. Import the time module.
2. Call the time() function of the time module to get the current time.
3. Convert the current time to a struct_time object using the localtime() function of the time module.
4. Extract the hour, minute, second, and microsecond components from the struct_time object.
5. Print the extracted components.
Python3
import time
current_time = time.time()
local_time = time.localtime(current_time)
hour = local_time.tm_hour
minute = local_time.tm_min
second = local_time.tm_sec
microsecond = int ((current_time - int (current_time)) * 1000000 )
print ( 'current hour' ,hour)
print ( 'current minute' ,minute)
print ( 'current second ' ,second)
print ( 'current microsecond ' , microsecond)
|
Outputcurrent hour 10
current minute 48
current second 50
current microsecond 397817
Time complexity: O(1)
Auxiliary Space: O(1)
Approach#4: Using strftime()
Use the time module to get the current time in seconds since the Epoch. Use the strftime function to format the time into hours, minutes, seconds, and microseconds. Convert the fractional part of the seconds into microseconds by subtracting the integer part and multiplying by 1,000,000. Print the formatted time.
Algorithm
1. Get the current time in seconds since the Epoch using time.time() function.
2. Use the time.strftime() function to convert the current time into the desired format.
3. Convert the fractional part of seconds into microseconds.
4. Print the formatted time.
Python3
import time
now = time.time()
hour, minute, second = time.strftime( '%H' ), time.strftime( '%M' ), time.strftime( '%S' )
microsecond = int ((now - int (now)) * 1000000 )
print (f "Current hour: {hour}" )
print (f "Current minute: {minute}" )
print (f "Current second: {second}" )
print (f "Current microsecond: {microsecond}" )
|
OutputCurrent hour: 05
Current minute: 17
Current second: 27
Current microsecond: 760716
Time complexity: O(1), as all operations take constant time.
Auxiliary Space: O(1), as we only store a few variables that have constant size.
Similar Reads
Python program to convert seconds into hours, minutes and seconds
Given an integer n (in seconds), convert it into hours, minutes and seconds. Examples: Input : 12345 Output : 3:25:45 Input : 3600 Output : 1:00:00 Approach #1 : Naive This approach is simply a naive approach to get the hours, minutes and seconds by simple mathematical calculations. C/C++ Code # Pyt
5 min read
Python program to print current year, month and day
In this article, the task is to write a Python Program to print the current year, month, and day. Approach: In Python, in order to print the current date consisting of a year, month, and day, it has a module named datetime. From the DateTime module, import date classCreate an object of the date clas
1 min read
Python program to find difference between current time and given time
Given two times h1:m1 and h2:m2 denoting hours and minutes in 24 hours clock format. The current clock time is given by h1:m1. The task is to calculate the difference between two times in minutes and print the difference between two times in h:m format. Examples: Input : h1=7, m1=20, h2=9, m2=45 Out
2 min read
Python program to convert time from 12 hour to 24 hour format
Given a time in 12-hour AM/PM format, convert it to military (24-hour) time. Now, Let's see How to Convert AM/PM to 24 Hour Time using Python. Note: Midnight is 12:00:00 AM on a 12-hour clock and 00:00:00 on a 24-hour clock. Noon is 12:00:00 PM on the 12-hour clock and 12:00:00 on the 24-hour clock.
3 min read
Measure time taken by program to execute in Python
Measuring the execution time of a Python program is useful for performance analysis, benchmarking, and optimization. Python provides several built-in modules to achieve this with ease. In this article, we'll explore different ways to measure how long a Python program takes to run. Using the time Mod
3 min read
How To Create a Countdown Timer Using Python?
In this article, we will see how to create a countdown timer using Python. The code will take input from the user regarding the length of the countdown in seconds. After that, a countdown will begin on the screen of the format 'minutes: seconds'. We will use the time module here. Step-by-Step Approa
2 min read
Get Current time in Python
In this article, we will know the approaches to get the current time in Python. There are multiple ways to get it. The most preferably date-time module is used in Python to create the object containing date and time. DateTime object in Python is used to manage operations involving time-based data. d
2 min read
Convert string to DateTime and vice-versa in Python
A common necessity in many programming applications is dealing with dates and times. Python has strong tools and packages that simplify handling date and time conversions. This article will examine how to effectively manipulate and format date and time values in Python by converting Strings to Datet
6 min read
Python - Time Strings to Seconds in Tuple List
Given Minutes Strings, convert to total seconds in tuple list. Input : test_list = [("5:12", "9:45"), ("12:34", ), ("10:40", )] Output : [(312, 585), (754, ), (640, )] Explanation : 5 * 60 + 12 = 312 for 5:12. Input : test_list = [("5:12", "9:45")] Output : [(312, 585)] Explanation : 5 * 60 + 12 = 3
7 min read
Date difference in minutes in Python
To calculate the date difference in minutes in Python, subtract two datetime objects to get the time difference. Then, convert that difference into minutes or break it down into minutes and seconds based on your needs. For example: for two dates, 2025-05-03 18:45:00 and 2025-05-03 16:30:00, the time
3 min read