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)
Related
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
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.
I would like to get the timestamp from dates in the following formats:
Mon, 23 Nov 2020 19:00:00 GMT
Mon, 23 Nov 2020 20:00:00 +0100
I am using the the following statements to convert dates to datetime objects:
dateobj = datetime.datetime.strptime(date, '%a, %d %b %Y %H:%M:%S %Z')
dateobj = datetime.datetime.strptime(date, '%a, %d %b %Y %H:%M:%S %z')
But using .timestamp() method, different seconds from epoch are printed. Why doesn't the %Z directive add timezone information to the datetime object? How could I get the timezone into account, so the timestamp is equal?
Please note Inconsistent datetime parse timezone in Python. Your problem is %Z, it makes strptime accept certain strings (GMT, UTC and any value in time.tzname - docs), but doesn't actually make anything out of it. The returned datetime object is naive - which is why Python will assume it's local time if you call the timestamp() method of it.
You can use dateutil's parser instead:
from dateutil.parser import parse
for s in ("Mon, 23 Nov 2020 19:00:00 GMT", "Mon, 23 Nov 2020 20:00:00 +0100"):
dt = parse(s)
print(repr(dt), dt.timestamp())
# datetime.datetime(2020, 11, 23, 19, 0, tzinfo=tzutc()) 1606158000.0
# datetime.datetime(2020, 11, 23, 20, 0, tzinfo=tzoffset(None, 3600)) 1606158000.0
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.
I have date in string:
Tue Oct 04 2016 12:13:00 GMT+0200 (CEST)
and I use (according to https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior):
datetime.strptime(datetime_string, '%a %b %m %Y %H:%M:%S %z %Z')
but I get error:
ValueError: 'z' is a bad directive in format '%a %b %m %Y %H:%M:%S %z %Z'
How to do it correctly?
%z is the +0200, %Z is CEST. Therefore:
>>> s = "Tue Oct 04 2016 12:13:00 GMT+0200 (CEST)"
>>> datetime.strptime(s, '%a %b %d %Y %H:%M:%S GMT%z (%Z)')
datetime.datetime(2016, 10, 4, 12, 13, tzinfo=datetime.timezone(datetime.timedelta(0, 7200), 'CEST'))
I also replaced your %m with %d; %m is the month, numerically, so in your case 04 would be parsed as April.
python datetime can't parse the GMT part (You might want to specify it manually in your format). You can use dateutil instead:
In [16]: s = 'Tue Oct 04 2016 12:13:00 GMT+0200 (CEST)'
In [17]: from dateutil import parser
In [18]: parser.parse(s)
Out[18]: d = datetime.datetime(2016, 10, 4, 12, 13, tzinfo=tzoffset(u'CEST', -7200))
In [30]: d.utcoffset()
Out[30]: datetime.timedelta(-1, 79200)
In [31]: d.tzname()
Out[31]: 'CEST'
Simpler way to achieve this without taking care of datetime formatting identifiers will be the usage of dateutil.parser(). For example:
>>> import dateutil.parser
>>> date_string = 'Tue Oct 04 2016 12:13:00 GMT+0200 (CEST)'
>>> dateutil.parser.parse(date_string)
datetime.datetime(2016, 10, 4, 12, 13, tzinfo=tzoffset(u'CEST', -7200))
If you want to parse all you datetime data in a column in pandas DataFrame, you can use apply method to apply together with dateutil.parser.parse to parse whole column:
from dateutil.parser import parse
df['col_name'] = df['col_name'].apply(parse)