跳至内容
菜单
此问题已终结
1 回复
278 查看

Hi guys!!!

Please i want to convert datetime to timestamp but i got this error:


date = 2012-10-29 11:38:22

^

SyntaxError: invalid syntax


Here is the code, please help

from datetime import datetime

import calendar

date = 2012-10-29 11:38:22

print(date)

print(calendar.timegm(date.utctimetuple()))

形象
丢弃
最佳答案

1351517902.0! :P

Try with:

from datetime import datetime
import time
# just to be sure about the format: 
fmt = '%Y-%m-%d %H:%M:%S'
source_date = '2012-10-29 11:38:22' 
correct_the_format = datetime.strptime(source_date, fmt) 
# convert to unix timestamp
data_ts = time.mktime(correct_the_format.utctimetuple())
print data_ts

Should work. Let me know.

 



形象
丢弃
相关帖文 回复 查看 活动
0
3月 25
1379
4
4月 24
174296
0
12月 23
2180
5
7月 25
228101
1
12月 22
3297