33

How to get timestamp from the structure datetime? What is right alternative for non-existing datetime.utcnow().timestamp()?

1

5 Answers 5

70

use time, and int to remove the milliseconds

from time import time 

int(time())

# 1561043225
Sign up to request clarification or add additional context in comments.

Comments

13
import time,datetime
time.mktime(datetime.datetime.today().timetuple())

1 Comment

Very cumbersome solution. As python. Many operations for such small work. C has time() and it works like a charm :)
10

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

9

If you don't have to get timestamp from structure datetime, you can decrease instruction like this

import time
print time.time()

1 Comment

This construction returns the time in seconds since the epoch as a floating point number, for example: 1558442772.1057804.
-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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.