Home >Backend Development >Python Tutorial >How to get current timestamp in Python?

How to get current timestamp in Python?

藏色散人
藏色散人Original
2019-03-27 15:59:4561825browse

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)].

How to get current timestamp in Python?

#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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn