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 22, 2018 in Python by ana1504.k
• 7,910 points
6,165 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 22, 2018 by SDeb
• 13,300 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, 2020 in Python by Roshni
• 10,520 points
684 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
• 58,090 points
648 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,210 points
672 views
0 votes
1 answer

How to convert date string to date object in python?

Use this :-  >>> datetime.datetime.strptime('24052010', "%d%m%Y").date() datetime.date(2010, 5, 24) Hope ...READ MORE

answered Oct 22, 2018 in Python by Priyaj
• 58,090 points
10,972 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,023 views
0 votes
1 answer
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,300 points
983 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 13, 2018 in Python by SDeb
• 13,300 points
1,234 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP