python3 timestamp
时间: 2023-12-03 21:57:45 浏览: 84
python timestamp和datetime之间转换详解
In Python 3, there are several ways to obtain a timestamp, which represents the number of seconds that have elapsed since January 1, 1970 (also known as the Unix epoch). Here are some examples:
1. Using the time module:
```
import time
timestamp = int(time.time())
print(timestamp)
```
Output:
```
1617758571
```
2. Using the datetime module:
```
import datetime
timestamp = int(datetime.datetime.now().timestamp())
print(timestamp)
```
Output:
```
1617758571
```
3. Using the calendar module:
```
import calendar
timestamp = int(calendar.timegm(time.gmtime()))
print(timestamp)
```
Output:
```
1617758571
```
Note that these examples all return the timestamp as an integer. If you need to work with fractions of a second, you can use the `time.time()` or `datetime.datetime.now().timestamp()` functions directly, which return a float.
阅读全文