[Python] python: datetime strftime not working properly

Discussão em 'Python' iniciado por Stack, Julho 3, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to print a date time into a file in a particular format due to which I have formatting a datetime object using strftime function as below:

    hFile = open("Test.csv", 'w')
    for str in datelist:
    splitStr = str.split("_")
    dateTime = datetime.datetime(int(splitStr[0]), int(splitStr[1]), int(splitStr[2]), int(splitStr[3]), int(splitStr[4]), int(splitStr[6]))
    hFile.write(dateTime.strftime('%H:%M %d/%m/%Y'))


    This for some of the cases works well and prints it in correct format but for some cases it prints it in the following format:

    >1/4/2013 12:45


    instead of

    >12:45 1/04/2013


    But when I only do:

    >dateTime.strftime('%H %d/%m/%Y')


    It doesn't switch the time and date in certain cases and prints it in correct format for all the cases. Any idea why is it happening?

    An Important observartion: Everything works fine when I remove the :. So, the below command works fine. Now that makes me more confused.

    >dateTime.strftime('%H %M %d/%m/%Y')


    works perfectly fine. Any idea what is the issue with putting : in the strftime?

    Thanks.

    Continue reading...

Compartilhe esta Página