Problem using strptime when there is no 0 padding - python

today = datetime.strptime('Sep 3 2021 2:58 pm', '%b %d %Y %I:%M %p')
Returns the following error: time data Sep 03 2021 2:58 pm does not match format %b %d %Y %I:%M %p

When I run it, it works fine. Perhaps you can try the following:
today = datetime.strptime('Sep 3 2021 2:58 pm', '%b %-d %Y %I:%M %p')
%-d should be used if the day number is not zero-padded (i.e. Sep 3 instead of Sep 03). According to this website this is platform-specific, so perhaps you are using a platform that requires you to specify it is not zero-padded.

Related

Date and time format from string

I'm converting the date of this string in this way, but I get the error "time data 'Aug 6, 2022, 10:44 AM' does not match format '%m %d, %Y, %I:%Mp'"
fechaDAT = 'Aug 6, 2022, 10:44 AM'
dateC = datetime.strptime(fechaDAT, "%m %d, %Y, %I:%Mp")
here is the right format :
dateC = datetime.strptime(fechaDAT, "%b %d, %Y, %I:%M %p")
%b for month abbrevation
%p for locale AM/PM

python : value error - timedate format error

I am getting the following error:
ValueError: time data 'Feb 1, 2017 0:03 pm' does not match format '%b %d, %Y %I:%M %p'
Here is the code :
from datetime import datetime
latest_datetime = 'Feb 1, 2017 0:03 pm'
datetime_obj = datetime.strptime(latest_datetime, "%b %d, %Y %I:%M %p")
I'm unable to figure out why I get the error.
A 12-hour clock has no 0 hour; %I will only match 1 through to 12. Your timestamp has an impossible time in it:
0:03 pm
From the strftime() and strptime() Behavior documentation:
%I
Hour (12-hour clock) as a zero-padded decimal number.
01, 02, ..., 12
Assuming 0 is really 12, you could repair this by replacing the ' 0:' with '12:' (note the leading space for the zero!):
>>> from datetime import datetime
>>> latest_datetime = 'Feb 1, 2017 0:03 pm'
>>> datetime.strptime(latest_datetime.replace(' 0:', '12:'), "%b %d, %Y %I:%M %p")
datetime.datetime(2017, 2, 1, 12, 3)
It doesn't really matter if you have one or two spaces between the year and the hour, the string will be parsed either way.

Django Python - Convert to datetime from Wed Dec 14 2016 14:39:16 GMT+0300 (AST)

I have a function in my backend and I receive the date from the front end in the formart Wed Dec 14 2016 14:39:16 GMT+0300 (AST)
date = request.body['date']
d = datetime.strptime(date, '%a %b %d %Y %X %Z%z')
I know strptime converts to the datetime object but I keep getting the following
'z' is a bad directive in format '%a %b %d %Y %X %Z%z'
What should my string format be?
You can do like this:
from dateutil import parser
parser.parse("Wed Dec 14 2016 14:39:16 GMT+0300 (AST)")

How to convert "Tue Aug 25 10:00:00 2015" this time stamp to "2015-08-25 10:00" in python

How to convert "Tue Aug 25 10:00:00 2015" this time stamp to ‍‍"2015-08-25 10:00" in python.
from datetime import datetime
date_object = datetime.strptime('Jun 1 2005 1:33PM', '%b %d %Y %I:%M%p')
With the correct format string, you can use datetime.strptime to parse the string and format it again:
import datetime
date = datetime.datetime.strptime('Tue Aug 25 10:00:00 2015', '%a %b %d %H:%M:%S %Y')
print date.strftime('%Y-%m-%d %H:%M')
use parser using pip install python-dateutil
>>>from dateutil import parser
>>>str(parser.parse("Tue Aug 25 10:00:00 2015"))
'2015-08-25 10:00:00'

python time string does not match format

def deadlines(t):
'''shows pretty time to deadlines'''
fmt = '%a %d %m %Y %I:%M %p %Z'
dt = datetime.strptime( t , fmt )
print 'dt ', repr(dt)
first = 'Sun 11 May 2014 05:00 PM PDT'
deadlines(first)
ValueError: time data 'Sun 11 May 2014 02:00 PM PDT' does not match format ' %a %d %m %Y %I:%M %p %Z '
Whats wrong with this?
%m matches months represent as a two-digit decimal (in [01, 12]). Use %b for abbreviated month names, or %B for full month names instead:
fmt = '%a %d %b %Y %I:%M %p %Z'
A table showing the date format directives and their meanings can be found here.
If you're having trouble parsing PDT using %Z:
Per the time.strptime docs:
Support for the %Z directive is based on the values contained in
tzname and whether daylight is true. Because of this, it is
platform-specific except for recognizing UTC and GMT which are always
known (and are considered to be non-daylight savings timezones).
So, if parsing the date string without PDT works:
In [73]: datetime.strptime('Sun 11 May 2014 05:00 PM', '%a %d %b %Y %I:%M %p')
Out[73]: datetime.datetime(2014, 5, 11, 17, 0)
but
datetime.strptime('Sun 11 May 2014 05:00 PM PDT', '%a %d %b %Y %I:%M %p %Z')
raises a ValueError, then you may need strip off the timezone name (they are, in general, ambiguous anyway):
In [10]: datestring = 'Sun 11 May 2014 05:00 PM PDT'
In [11]: datestring, _ = datestring.rsplit(' ', 1)
In [12]: datestring
Out[12]: 'Sun 11 May 2014 05:00 PM'
In [13]: datetime.strptime(datestring, '%a %d %b %Y %I:%M %p')
Out[13]: datetime.datetime(2014, 5, 11, 17, 0)
or use dateutil:
In [1]: import dateutil.parser as parser
In [2]: parser.parse('Sun 11 May 2014 05:00 PM PDT')
Out[2]: datetime.datetime(2014, 5, 11, 17, 0)

Categories