![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get min, seconds and milliseconds from datetime.now() in Python?
In this article, we will look at different ways to get minutes, seconds, and milliseconds from datetime now in Python.
The datetime.now() method is used to get the current minutes, seconds, and milliseconds. This is defined under the datetime module.
Syntax
The syntax of now() method is as follows −
datetime.now()
Returns the current date and time in time format.
Using the datetime.now() and epoctime()
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 formate string in form of “minutes : second. milliseconds”.
Example
The following is an example code to get 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 in Minutes:Seconds:Milliseconds is",formatted_time)
Output
The output of the above example code is as follows;
Formatted time in Minutes:Seconds:Milliseconds is 11:40.325948
Using the datetime.now() method to retrieve data
Here we use the datetime.now() method to get the current minutes, seconds, and milliseconds. The now() function is defined under the datetime module. And retrieve the current minutes, seconds, and milliseconds by using .minute, .second, and .microsecond respectively.
Example
In the following example code, 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("Milliseconds",now.microsecond)
Output
The output of the above example code is as follows;
Today's date is: 2022-09-05 10:12:31.476711 Minutes 12 Seconds 31 Milliseconds 476711