Bablu
Bablu

Reputation: 459

how to convert todays datetime into timestamp

I am tryng to convert date-time into string.

I am searching answer of this question

when i do this

time.mktime(time.strptime('2017-05-01 14:07:19',  '%Y-%m-%d %H:%M:%S'))

i am able convert datetime into time-stamp but I want to concert today's date into time-stamp like this:

timenow = datetime.datetime.now()
//timenow = 2017-05-01 14:07:19
time.mktime(time.strptime(timenow,  '%Y-%m-%d %H:%M:%S'))

then it throws error TypeError: expected string or buffer even i tried like

 time.mktime(time.strptime(str(timenow),  '%Y-%m-%d %H:%M:%S'))

then it throws ValueError: unconverted data remains: .067000

How could i convert todays datetime into timestamp

Upvotes: 0

Views: 2657

Answers (1)

alex7z
alex7z

Reputation: 113

import datetime

now = datetime.datetime.now()

print(now.strftime("%Y-%m-%d %H:%M:%S"))
print(now.timestamp())

Upvotes: 3

Related Questions