-1

I have the data and in that data, time date format is of type 2017-11-01 00:00:00-04:00. I want to strip -4:00 and have to use the remaining.

I did everything but no successs.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
spiispree
  • 1
  • 2

1 Answers1

0
from datetime import datetime
date_string = '2017-11-01 00:00:00-04:00'
date = datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S%z')

print(date)
# Returns: 2017-11-01 00:00:00-04:00

print(date.strftime("%Y-%m-%d %H:%M:%S"))
# Retruns: 2017-11-01 00:00:00
Jeromba6
  • 383
  • 1
  • 2
  • 7
  • Thanks. I have a column so how to change the column in this format? – spiispree Dec 18 '22 at 00:57
  • df["date"]=pd.to_datetime(df["date"],format="%Y-%m-%d %H:%M:%S%z"). I used this still it's not converting – spiispree Dec 18 '22 at 01:31
  • OP's apparently using Pandas, which has more powerful tools for working with datetimes. Check out the question Nick linked: [How to remove timezone from a Timestamp column in a pandas dataframe](/q/49198068/4518341) – wjandrea Dec 18 '22 at 02:29
  • You can remove the tz_localize from the dtype this can be done as follows: df['date'].dt.tz_localize(none) – Jeromba6 Dec 18 '22 at 13:23