Home >Backend Development >Python Tutorial >How to get current timestamp in Python?
In Python, you can use functions from the module time, datetime or calendar to obtain the current timestamp. Code statements such as [import time;ts = time.time() print(ts)].
#In Python, there are multiple ways to get the current timestamp. If you wish to obtain a timestamp in Python, you can use functions from the modules time, datetime or calendar.
Using the module time
The module time provides various time-related functions. One of them is time, which returns the number of seconds since the epoch.
import time; ts = time.time() print(ts) # 1553577699.47
Using the module datetime
The module datetime provides classes for manipulating dates and times in a more object-oriented way. One of them is date.datetime. Now returns the number of seconds since the epoch.
import datetime; ts = datetime.datetime.now().timestamp() print(ts) # 1553577699.47
Using the module calendar
Another way to get the current timestamp is to combine multiple functions from multiple modules. Python also provides the calendar module. In this example we will use the function calendar. Converts a tuple representing the current time.
import calendar; import time; ts = calendar.timegm(time.gmtime()) print(ts) # 1553577699
Related recommendations: "Python Tutorial"
The above is the detailed content of How to get current timestamp in Python?. For more information, please follow other related articles on the PHP Chinese website!