Date Time split in python

前端 未结 5 1583
再見小時候
再見小時候 2021-02-07 22:58

I have to split a date time which I get from a software in the below format to separate variables (year,month,day,hour, min,sec)

19 Nov 2015  18:45:00.000
         


        
5条回答
  •  北恋
    北恋 (楼主)
    2021-02-07 23:35

    As an alternative to wim's answer, if you don't want to install a package, you can do it like so:

    import datetime
    
    s = "19 Nov 2015  18:45:00.000"
    d = datetime.datetime.strptime(s, "%d %b %Y  %H:%M:%S.%f")
    
    print d.year
    print d.month
    print d.day
    print d.hour
    print d.minute
    print d.second
    

    This outputs:

    2015
    11
    19
    18
    45
    0
    

    This utilizes strptime to parse the string.

提交回复
热议问题