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
• 6,710 points
59 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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
• 10,860 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,140 points
28 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
• 66,910 points
30 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,140 points
57 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,500 points

edited Dec 12, 2018 by Nymeria 28 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
56 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
• 10,860 points
61 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
• 10,860 points
38 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.