Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove timezone information from datetime object

Tags:

python

What is the fastest way to trim datetime object of this form 2016-12-14 15:57:16.140645 to become like this: 2016-12-14 15:57:16? doing str('2016-12-14 15:57:16.140645').strip(".")[0] is painfully slow for large datasets and besides I need the returned format to be a datetime object

like image 335
DevEx Avatar asked Dec 15 '16 14:12

DevEx


1 Answers

For those that came here for the actual question 'Remove timezone information from datetime object', the answer would be something like: datetimeObject.replace(tzinfo=None)

from datetime import datetime, timezone
import time

datetimeObject = datetime.fromtimestamp(time.time(), timezone.utc)
print(datetimeObject)
datetimeObject = datetimeObject.replace(tzinfo=None)
print(datetimeObject)
like image 184
jtromans Avatar answered Sep 28 '22 04:09

jtromans