Search code examples
datetimeisopython-dateutil

How to parse ISO string?


It doesn’t get parsed and just prints out the same ISO string that I’m trying to parse: 2/12/17 00:00:52

What could I be doing wrong? Thank you in advance and will be sure to vote up and accept the answer


Solution

  • This happens because the representation of a datetime variable (that is the result of dateutil.parser.parse, btw) is to print the ISO representation of the date.

    However, if you store the variable instead of simply printing it after parsing it, you can print each individual part of the date:

    date = dateutil.parser.parse("2017-02-16 22:11:05+00:00")
    print(date.year)
    2017
    
    print(date.month)
    2
    
    print(date.day)
    16
    
    print(date.hour)
    22
    
    print(date.minute)
    11
    
    print(date.second)
    5
    

    Cheers!