Skip to content

Python Time Function | TimeStamp | Time Milli Seconds Now

  • by

Python Time Function is used for getting the time. In python to use the time function, you need to import time modules, no third-party library required. The python time function returns the time in seconds floating-point number since the epoch, in UTC

Python Time Function | TimeStamp | Time Milli Seconds Now example

Syntax

You have to import the time module to get time.

import time
time.time()

Note: no parameters are required, but in some cases you needed a format.

Python Time Function Example 

import time

# Time in second
print("Time : ", time.time())

# Time with associated attribute and values
print(time.localtime(time.time()))

# Get local date format
print(time.asctime(time.localtime(time.time())))

Output: Time : 1538973413.1055639
time.struct_time(tm_year=2018, tm_mon=10, tm_mday=8, tm_hour=10, tm_min=6, tm_sec=53, tm_wday=0, tm_yday=281, tm_isdst=0)
Mon Oct 8 10:06:53 2018

Python Time Function | TimeStamp | Time Milli Seconds Now example output

Get the Hour, Minutes and Seconds

Here is an example of getting, How to get an Hour, Minutes, and seconds in using python time function?

import time

# Time with associated attribute and values

timeAll = time.localtime(time.time())
print("Hour: ", timeAll.tm_hour)
print("Minutes: ", timeAll.tm_min)
print("Seconds: ", timeAll.tm_sec)

Output: Hour: 12
Minutes: 17
Seconds: 10

Get the Year, Month and Day

With Time function you can get the Year, Month, and Day.

import time

# Time with associated attribute and values

timeAll = time.localtime(time.time())
print("Year: ", timeAll.tm_year)
print("Month: ", timeAll.tm_mon)
print("Day: ", timeAll.tm_mday)

Output: Year: 2018
Month: 10
Day: 8

Python time milliseconds 

To get a time in milliseconds you have to multiple in time object, like this example.

import time

timestamp = int(time.time() * 1000.0)
print('Milliseconds ', timestamp)

Output: Milliseconds 1538990568714

Do comment if you have any doubts and suggestions on this tutorial.

Note: This example (Project) is developed in PyCharm 2018.2 (Community Edition)
JRE: 1.8.0
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.6

Python 3.7

All examples are in Python 3, so it may change its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading