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,870 points
244 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,180 points

Related Questions In Python

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
• 56,900 points
64 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
• 67,660 points
51 views
0 votes
1 answer

How to convert date string to date object in python?

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

answered Oct 22, 2018 in Python by Priyaj
• 56,900 points
261 views
0 votes
1 answer

How to convert an integer to a string using Python?

Here is an easy solution: def numberToBase(n, b): ...READ MORE

answered Nov 23, 2018 in Python by Nymeria
• 3,520 points

edited Dec 12, 2018 by Nymeria 48 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 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 330 views
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,180 points
94 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,180 points
58 views