How to get timestamp from the structure datetime? What is right alternative for non-existing datetime.utcnow().timestamp()
?
Asked
Active
Viewed 6.1k times
5 Answers
60
use time
, and int
to remove the milliseconds
from time import time
int(time())
# 1561043225

Alex
- 5,759
- 1
- 32
- 47
12
import time,datetime
time.mktime(datetime.datetime.today().timetuple())

galaxyan
- 5,944
- 2
- 19
- 43
-
1Very cumbersome solution. As python. Many operations for such small work. C has time() and it works like a charm :) – kyb Sep 02 '16 at 14:11
9
If you don't have to get timestamp from structure datetime, you can decrease instruction like this
import time
print time.time()

Damotorie
- 586
- 7
- 25
-
This construction returns the time in seconds since the epoch as a **floating point number**, for example: `1558442772.1057804`. – Prisacari Dmitrii May 21 '19 at 12:48
-3
If I understand correctly what sort of output you are seeking:
from datetime import datetime
timestamp = datetime.now().strftime("%H:%M:%S")
print(timestamp)
> 11:44:40
EDIT: Appears I misinterpreted your question? You are asking for the naive universal time, then galaxyan's answer is concise.

Konchshell
- 102
- 2
- 11