How to get timestamp from the structure datetime? What is right alternative for non-existing datetime.utcnow().timestamp()?
-
1This answer below is much simpler than the accepted: stackoverflow.com/a/56689007/3559330mattyb– mattyb2021-05-21 23:20:51 +00:00Commented May 21, 2021 at 23:20
Add a comment
|
5 Answers
import time,datetime
time.mktime(datetime.datetime.today().timetuple())
1 Comment
kyb
Very cumbersome solution. As python. Many operations for such small work. C has time() and it works like a charm :)
There is another stupid trick - achieve timedelta
(datetime.utcnow()-datetime(1970,1,1,0,0,0)).total_seconds()
found here. Better
(datetime.utcnow()-datetime.fromtimestamp(0)).total_seconds()
And this solution contains subseconds.
Comments
If you don't have to get timestamp from structure datetime, you can decrease instruction like this
import time
print time.time()
1 Comment
Prisacari Dmitrii
This construction returns the time in seconds since the epoch as a floating point number, for example:
1558442772.1057804.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.