Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to get min, seconds and milliseconds from datetime.now() in Python?
Python's datetime module is used to extract various components of the current date and time, such as minutes, seconds, and even milliseconds.
The datetime.now() method defined in the datetime module returns the current local date and time as a datetime object. This object allows us to access its individual components like minute, second, and microsecond.
Using Attributes of datetime.now() to Extract Values
Here we use the datetime.now() method to get the current minutes, seconds, and milliseconds. The now() function is defined under the datetime module. We retrieve the current minutes, seconds, and milliseconds by using .minute, .second, and .microsecond, respectively.
Example
In the following example, we get the current minutes, seconds, and milliseconds using the datetime.now() method −
from datetime import datetime
now = datetime.now()
print("Today's date is:", now)
print("Minutes:", now.minute)
print("Seconds:", now.second)
print("Microseconds:", now.microsecond)
print("Milliseconds:", now.microsecond // 1000)
The output of the above code is −
Today's date is: 2024-01-15 14:25:42.378945 Minutes: 25 Seconds: 42 Microseconds: 378945 Milliseconds: 378
Using datetime.now() and strftime() to Format Time
Here we use the strftime() method, which is provided by the datetime module. We have used the datetime.now() method to get the current date. Then we format this date by using the strftime() method. In this case, we format a string in the form of "minutes:seconds.microseconds".
Example
The following example gets the current minutes, seconds and milliseconds using the datetime.now() and strftime() methods −
from datetime import datetime
curr_time = datetime.now()
formatted_time = curr_time.strftime('%M:%S.%f')
print("Formatted time (MM:SS.microseconds):", formatted_time)
# To get milliseconds, truncate microseconds
milliseconds_format = curr_time.strftime('%M:%S.') + str(curr_time.microsecond // 1000).zfill(3)
print("Formatted time (MM:SS.milliseconds):", milliseconds_format)
The output of the above code is −
Formatted time (MM:SS.microseconds): 25:42.378945 Formatted time (MM:SS.milliseconds): 25:42.378
Creating a Custom Time String with Minutes, Seconds, and Milliseconds
Sometimes, we may want to create a custom time string using the values obtained from datetime.now() method. We can combine .minute, .second, and the converted .microsecond for this.
Example
The following code gets the current time, extracts minutes, seconds, and converts microseconds to milliseconds, then formats it as a string −
from datetime import datetime
now = datetime.now()
minutes = now.minute
seconds = now.second
milliseconds = now.microsecond // 1000
time_str = f"{minutes:02d}:{seconds:02d}.{milliseconds:03d}"
print("Time (MM:SS.ms):", time_str)
# Alternative format
time_str_alt = f"Minutes: {minutes}, Seconds: {seconds}, Milliseconds: {milliseconds}"
print("Alternative format:", time_str_alt)
The output of the above code is −
Time (MM:SS.ms): 25:42.378 Alternative format: Minutes: 25, Seconds: 42, Milliseconds: 378
Key Points
- microsecond vs millisecond: Python's datetime returns microseconds (1/1,000,000 second). To get milliseconds, divide by 1000.
-
strftime() format codes: Use
%Mfor minutes,%Sfor seconds, and%ffor microseconds. -
Zero padding: Use format specifiers like
{value:02d}to ensure consistent width.
Conclusion
Use datetime.now() attributes to directly access time components. Convert microseconds to milliseconds by dividing by 1000. Use strftime() for formatted string output or f-strings for custom formatting.
