Convert unicode to datetime proper strptime format - python

I am trying to convert a unicode object to a datetime object.
I read through the documentation: http://docs.python.org/2/library/time.html#time.strptime
and tried
datetime.strptime(date_posted, '%Y-%m-%dT%H:%M:%SZ')
but I get the error message ValueError: time data '2014-01-15T01:35:30.314Z' does not match format '%Y-%m-%dT%H:%M:%SZ'
Any feedback on what is the proper format?
I appreciate the time and expertise.

You can parse the microseconds:
from datetime import datetime
date_posted = '2014-01-15T01:35:30.314Z'
datetime.strptime(date_posted, '%Y-%m-%dT%H:%M:%S.%fZ')

One option is to let dateutil do the job:
>>> from dateutil import parser
>>> parser.parse('2014-01-15T01:35:30.314Z')
datetime.datetime(2014, 1, 15, 1, 35, 30, 314000, tzinfo=tzutc())

Related

convert 2021-01-18T11:18:10.833876+00:00 to datetime python

I am trying to convert the following string to datetime in python. After referring to datetime.strptime(‘2017-01-12T14:12:06.000-0500’,'%Y-%m-%dT%H:%M:%S.%f%Z') I am trying the below mentioned format.
config_current_ts = datetime.strptime(internal_config["timestamp_str"], "%Y:%m:%dT%H:%M:%S.%f%z")
Yet I get an error stating :
ValueError: time data '2021-01-18T11:18:10.833876+00:00' does not
match format '%Y:%m:%dT%H:%M:%S.%f%z'
I am using python3.7.4. Can someone tell me how to convert this to datetime? I want to basically compare the string and current time to see which is ahead.
With Python 3.7+, use fromisoformat - since you have ISO 8601 format, it is appropriate and as a benefit also more efficient. Ex:
from datetime import datetime
s = '2021-01-18T11:18:10.833876+00:00'
dt = datetime.fromisoformat(s)
print(dt)
# 2021-01-18 11:18:10.833876+00:00
print(repr(dt))
# datetime.datetime(2021, 1, 18, 11, 18, 10, 833876, tzinfo=datetime.timezone.utc)
the format string should be '%Y-%m-%dT%H:%M:%S.%f%z'
>>> from datetime import datetime
>>> d = '2021-01-18T11:18:10.833876+00:00'
>>> datetime.strptime(d, '%Y-%m-%dT%H:%M:%S.%f%z')
datetime.datetime(2021, 1, 18, 11, 18, 10, 833876, tzinfo=datetime.timezone.utc)

convert str date to datetime for timezone

I have an API that return this date string
strdate = '2019-10-07T06:09:28.984Z'
How do I convert it to a datetime object?
dt = datetime.datetime.strptime(strdate, '%Y-%m-%d')
If I use strptime like above i get "ValueError: unconverted data remains: T06:09:54.346Z"
What do I do if there is "T" and "Z" included in the string date? I want the data in local time. But is the string really timezone aware so I can convert it properly? In this case I know the timezone, but what if I did not know?
The error is because you're not including the time part in the format string. Do that:
datetime.strptime('2019-10-07T06:09:28.984Z', '%Y-%m-%dT%H:%M:%S.%f%z')
This results in:
datetime.datetime(2019, 10, 7, 6, 9, 28, 984000, tzinfo=datetime.timezone.utc)
If you want to convert this to a local timezone, do that:
from pytz import timezone
dt = datetime.strptime(strdate, '%Y-%m-%dT%H:%M:%S.%f%z')
local_dt = dt.astimezone(timezone('Asia/Tokyo'))
Demo: https://repl.it/repls/RotatingSqueakyCertifications
import datetime
strdate = '2019-10-07T06:09:28.984Z'
dt=datetime.datetime.strptime(strdate, "%Y-%m-%dT%H:%M:%S.%fZ")
print(dt)
# output - 2019-10-07 06:09:28.984000

I want to parse string to timestamp-with-timezone in python

I try to parse string to time-stamp with timezone format.
here is an example
"2016-02-18 16:13:07+09"
i want to know parsing this string format to time-stamp format in python.
how can i do that?
Is the UTC offset format in your string +09 or +0900 ?
If the offset in your string is 0900 you can use the below .If your UTC offset is only +09 as you mentioned in your question , you can pad the string with 00 and get the below code to work .
Code:
import datetime
time="2016-02-18 16:13:07+0900"
new_time=datetime.datetime.strptime(time,"%Y-%m-%d %H:%M:%S%z")
print(new_time)
new_time_python=datetime.datetime.strftime(new_time,"%m-%d-%y")
print(new_time_python)
Output
2016-02-18 16:13:07+09:00
02-18-16
dateutil might be a suitable library for your purposes:
from dateutil.parser import parser
p = parser()
d = p.parse('2016-02-18 16:13:07+09'.decode('utf-8')) # must be unicode string
d
>>> datetime.datetime(2016, 2, 18, 16, 13, 7, tzinfo=tzoffset(None, 32400))
If the UTC offset may be specified both as +HH and +HHMM format then you could use str.ljust() method to normalize the input time string. Then you could use .strptime() to parse it:
#!/usr/bin/env python3
from datetime import datetime
time_string = "2016-02-18 16:13:07+09"
dt = datetime.strptime(time_string.ljust(24, "0"), "%Y-%m-%d %H:%M:%S%z")
# -> datetime.datetime(2016, 2, 18, 16, 13, 7,
# tzinfo=datetime.timezone(datetime.timedelta(0, 32400)))
If your Python version doesn't support %z, see How to parse dates with -0400 timezone string in python?

Convert UTC time to python datetime

I have numerous UTC time stamps in the following format:
2012-04-30T23:08:56+00:00
I want to convert them to python datetime objects but am having trouble.
My code:
for time in data:
pythondata[i]=datetime.strptime(time,"%y-%m-%dT%H:%M:%S+00:00")
I get the following error:
ValueError: time data '2012-03-01T00:05:55+00:00' does not match format '%y-%m-%dT%H:%M:%S+00:00'
It looks like I have the proper format, so why doesn't this work?
Change the year marker in your time format string to %Y:
time = '2012-03-01T00:05:55+00:00'
datetime.strptime(time, "%Y-%m-%dT%H:%M:%S+00:00")
# => datetime.datetime(2012, 3, 1, 0, 5, 55)
See strftime() and strptime() behavior.
I highly recommend python-dateutil library, it allows conversion of multiple datetime formats from raw strings into datetime objects with/without timezone set
>>> from dateutil.parser import parse
>>> parse('2012-04-30T23:08:56+00:00')
datetime.datetime(2012, 4, 30, 23, 8, 56, tzinfo=tzutc())

parsing date string in python (convert string to date)

I have a datetime string in the form of a string as:
2011-10-23T08:00:00-07:00
How do i parse this string as the datetime object.
I did the following reading the documentation:
date = datetime.strptime(data[4],"%Y-%m-%d%Z")
BUt I get the error
ValueError: time data '2011-10-23T08:00:00-07:00' does not match format '%Y-%m-%d%Z'
which is very clear.
But I am not sure how to read this format.
Any suggestions.
Thanks
Edit: Also, I must add, all I care about is the date part
Standard datetime.datetime.strptime has problems with timezone definitions. Use dateutil.parser
>>> from dateutil import parser
>>> parser.parse("2011-10-23T08:00:00-07:00")
datetime.datetime(2011, 10, 23, 8, 0, tzinfo=tzoffset(None, -25200))
If you care about the date part only, you can try it without dateutil.parser:
>>> from datetime import datetime
>>> datetime.strptime(data[4].partition('T')[0], '%Y-%m-%d').date()
datetime.date(2011, 10, 23)

Categories