How to get current timestamp in Python?

藏色散人
Release: 2020-09-10 13:26:51
Original
61616 people have browsed it

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
Copy after login

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
Copy after login

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!