How can I convert 'Jan' to an integer using Datetime? When I try strptime, I get an error time data 'Jan' does not match format '%m'
Asked
Active
Viewed 4.2k times
17

huhh hhbhb
- 573
- 3
- 7
- 19
-
1Note that `strptime` is affected by your locale setting – John La Rooy Aug 03 '15 at 21:16
-
`%m` is month as number. I think you want `%b`. see: http://strftime.org/ – NightShadeQueen Aug 03 '15 at 21:39
-
possible duplicate of [python convert string to datetime](http://stackoverflow.com/questions/4617267/python-convert-string-to-datetime) – Mark Aug 03 '15 at 22:41
4 Answers
25
You have an abbreviated month name, so use %b
:
>>> from datetime import datetime
>>> datetime.strptime('Jan', '%b')
datetime.datetime(1900, 1, 1, 0, 0)
>>> datetime.strptime('Aug', '%b')
datetime.datetime(1900, 8, 1, 0, 0)
>>> datetime.strptime('Jan 15 2015', '%b %d %Y')
datetime.datetime(2015, 1, 15, 0, 0)
%m
is for a numeric month.
However, if all you wanted to do was map an abbreviated month to a number, just use a dictionary. You can build one from calendar.month_abbr
:
import calendar
abbr_to_num = {name: num for num, name in enumerate(calendar.month_abbr) if num}
Demo:
>>> import calendar
>>> abbr_to_num = {name: num for num, name in enumerate(calendar.month_abbr) if num}
>>> abbr_to_num['Jan']
1
>>> abbr_to_num['Aug']
8

Martijn Pieters
- 1,048,767
- 296
- 4,058
- 3,343
-
-
@huhhhhbhb: yes, what did you expect? If you don't include a year or day, the rest of the values fall back to defaults. – Martijn Pieters Aug 03 '15 at 21:11
-
True, I really just wanted to get a single digit corresponding to the month number – huhh hhbhb Aug 03 '15 at 21:12
-
2@huhhhhbhb To get a month number from datetime instance use `datetime.strptime('Feb', '%b').month` which gives `2`. – AXO Jan 26 '17 at 06:50
-
is it more efficient to create and use a dict to map full month names (%B) to month numbers (%m)? The input I'm working with is formatted like such: `'Month ##, ####'` – oldboy Jul 21 '18 at 15:53
-
1@Anthony: then you have more than just a full month name and using `datetime.strptime()` makes sense. – Martijn Pieters Jul 21 '18 at 16:43
-
@MartijnPieters i have error when executing this code `AttributeError: type object 'datetime.datetime' has no attribute 'datetime'` – Anas Fanani Jan 13 '21 at 00:30
-
@LexaVey: Nowhere in my code am I using `datetime.datetime` (don't confuse the REPL echoing output of expressions with the expressions themselves). If you used `from datetime import datetime` and then are using `datetime.datetime` somewhere in your code, you simply need to remove the leading `datetime.`. Don't confuse the _module_ with the _class defined in the module_. – Martijn Pieters Jan 13 '21 at 11:33
1
Off the cuff-
Did you try %b
?

AndrewSmiley
- 1,933
- 20
- 32
-
1This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – J Richard Snape Aug 03 '15 at 21:52
-
2Sure, it came to me in a review queue and that is a standard message for when further info is requested in an answer. If you edit it to say - e.g. - "you need to use %b", then it likely wouldn't be flagged for review. – J Richard Snape Aug 03 '15 at 23:44
-
Oooh I see. Didn't realize that was a standardized message. Thanks! – AndrewSmiley Aug 04 '15 at 01:04
1
This is straightforward enough that you could consider just using a dictionary, then you have fewer dependencies anyway.
months = dict(Jan=1, Feb=2, Mar=3, ...)
print(months['Jan'])
>>> 1

BlivetWidget
- 10,543
- 1
- 14
- 23
1
from calendar import month_abbr
month = "Jun"
for k, v in enumerate(month_abbr):
if v == month:
month = k
break
print(month)
6
You will get the number of month 6

Kernel
- 661
- 7
- 10