SoFunction
Updated on 2024-11-15

Python code to read the current time with precision to the second

Import the time package to get the time of day.

# -*- coding: UTF-8 -*-

import time

 

print(())

# exports:1562304217.5744529

After executing the above code we find that we get a timestamp which is based on the seconds data from 1970 to the present time

But a lot of people's needs don't want a timestamp, but something that everyone can read (year, month, day, hour, minute, second)

So we need to compute this timestamp, a minute is 60 seconds, so we have to count the minutes, and these are modulo arithmetic operations that can be used to solve this problem. But someone has already done this calculation for us, we just need to use it like this (on the code):

# -*- coding: UTF-8 -*-

import time

 

print("Current time: ",('%Y.%m.%d %H:%M:%S ',(())))

# exports:current time: 2019.07.05 13:23:04

Content Expansion

Python3 gets the current time (to the second)

Output by format:

print("Current time: ",('%Y.%m.%d %H:%M:%S ',(())))