How to convert string into epoch time?

0 votes

I tried to convert a string into epoch time in the following code :

from datetime import datetime
from dateutil import parser

dt = '2018-07-011 00:00:00'
dt = parser.parse(dt)
print(dt)

print (datetime(dt).timestamp())

But it gives the error:

print (datetime(dt).timestamp())
   TypeError: an integer is required (got type datetime.datetime)

how do I convert it without any error?

Sep 21, 2018 in Python by ana1504.k
• 7,890 points
1,419 views

1 answer to this question.

0 votes

you are passing the parsed datetime object to the datetime constructor(datetime(dt))which expects integers.

 use your data time instance as :

print (dt.timestamp())

From the docs:

Return POSIX timestamp corresponding to the datetime instance.

answered Sep 21, 2018 by SDeb
• 13,250 points

Related Questions In Python

0 votes
1 answer

How to convert each list item into string in a column of data frame. ?

Hey, To split a string you can use ...READ MORE

answered Feb 5 in Python by Roshni
• 600 points
117 views
0 votes
1 answer

How to split a string into a list?

You can use the function  text.split() This should be ...READ MORE

answered Jul 30, 2018 in Python by Priyaj
• 57,530 points
125 views
+1 vote
2 answers

How to convert list to string

mylist = [1, 2, 3] ‘’.join(map(str, mylist)) ==> ...READ MORE

answered Aug 21, 2018 in Python by Omkar
• 69,060 points
90 views
0 votes
1 answer

How to convert date string to date object in python?

Use this :-  >>> datetime.datetime.strptime('2405201 ...READ MORE

answered Oct 22, 2018 in Python by Priyaj
• 57,530 points
3,391 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 962 views
0 votes
0 answers
0 votes
1 answer

How to convert a Unicode string to string

It can be done in the following ...READ MORE

answered Oct 16, 2018 in Python by SDeb
• 13,250 points
186 views
0 votes
1 answer

How do I convert a Python program to a runnable .exe Windows program?

Understand that every 'freezing' application for Python ...READ MORE

answered Oct 12, 2018 in Python by SDeb
• 13,250 points
131 views