TypeError: an integer is required (got type datetime.datetime) when trying to convert a datetime object within a dictionary to a string

18,490

I see now what is happening. The line d = datetime.datetime(value) is the issue, you are passing a datetime object to the datetime.datetime() method which is why you get the valueError. This is unnecessary because you are already getting a datetime object out of your dictionary, so there is no reason to construct a new one. Here's the fixed code:

import datetime

dic = {'Select start date': datetime.datetime(2019, 11, 7, 0, 0)}

for key, datetime_obj in dic.items():
     datetime_str = datetime_obj.strftime("%d %b %Y")
     print(datetime_str)

An easy way to debug this is to use type(). So for example:

for key, value in dic.items():
    print(type(value))

will give you <classdatetime.datetime>. Looking up the documentation for datetime.datetime() will tell you that this method only accepts integers.

Share:
18,490

Related videos on Youtube

츄 plus
Author by

츄 plus

I'm a web robot. Be nice to me, and I will CAPTCHA your heart.

Updated on June 04, 2022

Comments

  • 츄 plus
    츄 plus almost 2 years

    So I'm working in Python 3.7.4 with user-inputted dates, that are stored by another program in a variables in a dictionary format. These can potentially be any date in the immediate past. For instance November 6th 2019 looks like this:

    {'Select start date': datetime.datetime(2019, 11, 6, 0, 0)}
    

    I don't care about the item label, but I'd like to convert this dictionary date to the format 06 Nov 2019 (which I know is strftime('%d %b %Y')) but I'm not sure how to make it understand that the above is a datetime object and do a conversion on it when it's actually a dictionary object, and without throwing up errors.

    I've read a lot on here about this but almost all questions here just look at either today's date (so datetime.datetime.now() is used), or a specific date, rather than a user-inputted date that could vary, and lives inside a dictionary. I've already seen plenty of stuff like this:

    import datetime
    d = datetime.datetime(2019, 11, 06, 0, 0, 0, 000000)
    d.strftime("%a %b %d %Y %H:%M:%S %z")
    

    ...but it doesn't seem to apply in exactly this case. Commands like strftime and strptime don't seem to work because of the dictionary format, and I can't use static examples like the above because the actual date that I want to convert won't always be the same. How can I make this work without going some crazy long way using string manipulation? I feel like there's a really easy solution to this that I'm missing.

    Code example (that doesn't work):

    import datetime
    
    dic = {'Select start date': datetime.datetime(2019, 11, 7, 0, 0)}
    
    for key, value in dic.items():
         d = datetime.datetime(value)
         d.strftime("%d %b %Y")   
    

    Produces the error:

    TypeError: an integer is required (got type datetime.datetime)
    
    • wim
      wim over 4 years
      Why do you want to do this? A datetime instance is better than a string in every way, you should only convert to string if you need to serialize it.
    • AMC
      AMC over 4 years
      I don’t understand why the strftime() method you mentioned wouldn’t work. Can you expand on that a bit more? I also want to confirm that when you say the actual date will change, you’re referring to just the date and not the format.
    • 츄 plus
      츄 plus over 4 years
      I'm doing this because another program (that I don't control) wants the date in that particular format. Yes I'm talking about the actual date changing i.e Nov 6th is just an example, but I do want to change the format of that date from datetime.datetime(2019, 11, 6, 0, 0) to Nov 6th 2019. strftime() doesn't seem to like it that the date is within a dictionary, trying to pull it out generates errors where an integer is expected, but I don't actually want an integer.
    • 츄 plus
      츄 plus over 4 years
      Edited to clarify the question a bit.