Obtain Python Current Time in Milliseconds

Home /

Table of Contents

To get current time

You can use the ‘datetime‘ module to get the current time in Python.

Python
import datetime
current_time = datetime.datetime.now().time()
print(current_time)

This will print the current date in the format ‘YYYY-MM-DD‘.

To get current date

You can use same module to get date.

Python
import datetime
current_date = datetime.datetime.now().date()
print(current_date)

This will print the current date in the format ‘YYYY-MM-DD‘.

To get current date and time both

You can use the ‘datetime‘ module to get both the current time and date in Python.

Python
import datetime
current_datetime = datetime.datetime.now()
print(current_datetime)

This will print the current date and time in the format ‘YYYY-MM-DD HH:MM:SS.ssssss‘.

Get time in millisecond

In Python, you can get the current time in milliseconds using the ‘time‘ module and its ‘time()‘ function, which returns the time in seconds since the Epoch (January 1, 1970, 00:00:00 UTC). You can then multiply the result by 1000 to get the time in milliseconds.

Here’s an example:

Python
import time

milliseconds = round(time.time() * 1000)

print(milliseconds)

This will output the current time in milliseconds.

Note that the ‘round()‘ function is used to round the result to the nearest integer value because the ‘time()‘ function returns the time as a floating-point number. If you don’t need to round the value, you can remove the ‘round()‘ function.

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.

Other Recommended Article