Unicode string to Date format conversion error - python

I am a beginner in python automation with selenium bindings.
I have a Unicode string in the format like Monday , December 12, 2016 , 00:00:23 PM.
I am using the below command
datetime.strptime(date, "%A, %B %d, %Y, %I:%M:%S %p")
But I am getting error that time data doesn't match the format.
Please help me on this issue.

I tried like this,
print datetime.datetime.strptime(u'Monday , DECEMBER 12, 2016 , 00:00:23 PM' , '%A , %B %d, %Y , %H:%M:%S %p')
and the output:
2016-12-12 00:00:23

Try
import time
time.strftime("%A, %B %d, %Y, %I:%M:%S %p", time.gmtime())
and make sure the space or other symbols are not missing.

Related

strptime() gives ValueError: time data 'AM CDT' does not match format '%p %Z'

I have a str that I want to convert to a datetime. This str is this: 'Thursday, September 9, 2021 at 11:50 AM CDT. I am using the datetime.strptime() function, but it seems like the AM or time zone is not being recognized.
When I use the code
time = 'Thursday, September 9, 2021 at 11:50 AM CDT'
time = datetime.strptime(time, '%A, %B %d, %Y at %I:%M %p %Z')
I get the following:
ValueError: time data 'Thursday, September 9, 2021 at 11:50 AM CDT' does not match format '%A, %B %d, %Y at %I:%M %p %Z:%M %p %Z'
I've been able to convert the first part up until the %p %Z part, at which I get the following error:
ValueError: time data 'AM CDT' does not match format '%p %Z'
Any ideas on how dt.strptime() can recognize AM/PM and the time zone correctly?
Not sure of the best approach, but since Python 3.9 you can use the ZoneInfo module for this:
from datetime import datetime
from zoneinfo import ZoneInfo
CDT = ZoneInfo('CST6CDT')
# or alternatively:
# CDT = ZoneInfo('US/Central')
time = 'Thursday, September 9, 2021 at 1:50 PM'
time = datetime.strptime(time, '%A, %B %d, %Y at %I:%M %p')
time = time.replace(tzinfo=CDT)
print(time) # 2021-09-09 13:50:00-05:00
#MrFuppes made a good point about an ambiguity between the CST6EDT and US/Central zones. However, when I tried a quick test in DST and outside of DST, I couldn't see any noticeable difference, as the time zone seemed to adjust automatically - which indicates that either of those zone values appear to be DST- aware (unless I'm missing something of course).
I added an example below:
from datetime import datetime
from zoneinfo import ZoneInfo
# CDT = ZoneInfo('CST6CDT')
CDT = ZoneInfo('US/Central')
# Note: DST in 2021, ends on November 7th
time_dst = 'Saturday, November 6, 2021 at 1:50 PM'
time_st = 'Monday, November 8, 2021 at 1:50 PM'
time_dst = datetime.strptime(time_dst, '%A, %B %d, %Y at %I:%M %p')
time_dst = time_dst.replace(tzinfo=CDT)
time_st = datetime.strptime(time_st, '%A, %B %d, %Y at %I:%M %p')
time_st = time_st.replace(tzinfo=CDT)
print('DST: ', time_dst)
print('ST: ', time_st)
Output appears to be the same despite which ZoneInfo object is used:
DST: 2021-11-06 13:50:00-05:00
ST: 2021-11-08 13:50:00-06:00

How to fix ValueError time date does not match format when converting from string to datetime

Im trying to convert a string to datetime and keep getting the error: ValueError: time data 'Mon, 22 Apr 2019 17:04:38 +0200 (CEST)' does not match format '%a, %d %b %Y %H:%M:%S %z %Z'
from datetime import datetime
s = "Mon, 22 Apr 2019 17:04:38 +0200 (CEST)"
d = datetime.strptime(s, '%a, %d %b %Y %H:%M:%S %z %Z')
What am i missing?
%Z is generally used for converting into string format. In any case, it is the offset, not the name of the time zone.
The rest of your code is valid, however:
s = "Mon, 22 Apr 2019 17:04:38 +0200"
d = datetime.strptime(s, '%a, %d %b %Y %H:%M:%S %z')
datetime only comes with the ability to parse UTC and whatever local time zone is listed in time.tzname. It can't match (CEST) because it doesn't know what timezone that is (It would also be redundant because you defined the timezone using the offset +0200).
You will need to implement your own (CEST) using datetime.tzinfo or by importing an external library like pytz or pendulum in order to parse (CEST) from a string into a datetime.timezone.
Also, don't forget to include parenthesis() in your match string.
This code passes, however, I do not know what happens to 'CEST' once it is converted into the string.
from datetime import datetime
tz = 'CEST'
s = "Mon, 22 Apr 2019 17:04:38 +0200 " + tz
d = datetime.strptime(s, '%a, %d %b %Y %H:%M:%S %z ' + tz)

ValueError: time data '13 Aug 05' does not match format '%d %m %y'

Not sure why I'm getting this error when trying to parse a string into datetime.
This is the code I have:
date = datetime.strptime("13 Aug 05", '%d %m %y')
and it is raising this error:
ValueError: time data '13 Aug 05' does not match format '%d %m %y'
date = datetime.strptime("13 Aug 05", '%d %b %y')
You need to use %b, not %m, because your string uses the month's 3-letter abbreviated name and not the zero-padded decimal number.

Python: Date Conversion Error

I'm trying to convert a string into a date format, to be later stored into an SQLite database. Below is the code line at which I'm getting an error.
date_object = datetime.strptime(date, '%b %d, %Y %H:%M %Z')
And this is the error:
File "00Basic.py", line 20, in spider
date_object = datetime.strptime(date, '%b %d, %Y %H:%M %Z') File "C:\Python27\lib\_strptime.py", line 332, in _strptime
(data_string, format)) ValueError: time data 'Aug 19, 2016 08:13 IST' does not match format '%b %d, %Y %H %M %Z'
Question 1: How do I resolve this error?
Question 2: Is this the right approach for preparing to store the date in SQLite later?
Please Note: Very new to programming.
You could use pytz for the timezone conversion as shown:
from datetime import datetime
from pytz import timezone
s = "Aug 19, 2016 08:13 IST".replace('IST', '')
print(timezone('Asia/Calcutta').localize(datetime.strptime(s.rstrip(), '%b %d, %Y %H:%M')))
#2016-08-19 08:13:00+05:30
#<class 'datetime.datetime'>
I would suggest you to use dateutil incase you are handling multiple timezones of string.
The problem is located in the %Z (Time zone) part of the format.
As the documentation explains
%Z Time zone name (empty string if the object is naive). (empty), UTC, EST, CST
It looks like only UTC,EST and CST are valid. (Or it just doesn't recognize IST)
In order to fix this, you could use the %z parameter that accepts any UTC offset, like so:
struct_time = time.strptime("Aug 19, 2016 08:13 +0530", '%b %d, %Y %H:%M %z')
Update: Although this works fine in Python +3.2 it raises an exception when it's run with Python2

Parsing long string as datetime

When using
import datetime
s = 'Sat Apr 23 2016 00:00:00 GMT+0100'
print datetime.datetime.strptime(s, "%a %m %d %y %H:%M:%S GMT+0100")
I get:
ValueError: time data 'Sat Apr 23 2016 00:00:00 GMT+0100' does not match format '%a %m %d %y %H:%M:%S GMT+0100'
How to parse such a string?
Note: Using dateutil.parser.parse didn't work : it produced some weird datetime object that I could not subtract with another datetime, i.e. d1 - d2 didn't work.
According to this reference,
the format should be "%a %b %d %Y %H:%M:%S GMT+0100"
Use this format string instead: "%a %b %d %Y %H:%M:%S GMT+0100".
I made two changes:
Replaced %m (Month as a zero-padded decimal number) with %b (Month as locale’s abbreviated name)
Replaced %y (Year without century as a zero-padded decimal number) with %Y (Year with century as a decimal number)

Categories