0

I am trying to convert string variable to date object the output doesnt have the UTC component.

Here is the sample code .. Appreciate your help!

import datetime
data="2016-01-19T00:00:00.000-0800"
result2 = datetime.datetime.strptime(data[0:23],"%Y-%m-%dT%H:%M:%S.%f")
print(result2)
totooooo
  • 1,050
  • 1
  • 12
  • 32
  • The code that you have provided works. What is the thing that doesn't work? You're mentioning a "UTC component". Do you mean that you want the timezone indicated as "-0800" in the string to be included in the datetime object returned? – totooooo Jan 14 '20 at 20:36

1 Answers1

0

That's because you are slicing out the utc part of the string in data[0:23], if you want to get a datetime object with timezone information you can do this instead:

import datetime

data = "2016-01-19T00:00:00.000-0800"
result2 = datetime.datetime.strptime(data,"%Y-%m-%dT%H:%M:%S.%f%z")
print(result2)

>>> 2016-01-19 00:00:00-08:00
marcos
  • 4,473
  • 1
  • 10
  • 24