For example, I have the int 043017, which I want converted to 04/30/17 (April 30, 2017), I want to be able to convert any int of that format into datetime, how can this be accomplished?
Asked
Active
Viewed 5,186 times
4
-
not *any int* will be valid in your case, for ex. `323216` – RomanPerekhrest Apr 30 '17 at 18:52
-
so is there a way to do it with my int, or astring version of the int? so far nothing works – Alex Apr 30 '17 at 19:12
1 Answers
6
import datetime
d = datetime.datetime.strptime(input, '%m%d%y')

mohammad
- 2,232
- 1
- 18
- 38
-
2This is the way to do it, although it requires a string as input. An integer will not work. @Alex you should convert the integer to a string, and then use this method. Note that you cannot keep the leading 0 in a python integer. (You could check the string length and a pad as necessary) – Deem Apr 30 '17 at 18:59
-
-
@Alex It's working fine for me with this string. You cant print d to see it. Do you get any error? – mohammad Apr 30 '17 at 19:13
-
ValueError: time data '043117' does not match format '%m%d%Y' this is all it says. i dont know why – Alex Apr 30 '17 at 19:17
-
1`%Y` is a four-digit year, so of course it doesn't match. `%y` is the format for a 2-digit year – jasonharper Apr 30 '17 at 19:20
-
-
-