Date Time split in python

前端 未结 5 1579
再見小時候
再見小時候 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:33

    Below solution should work for you:

    import datetime
    
    string = "19 Nov 2015  18:45:00.000"
    date = datetime.datetime.strptime(string, "%d %b %Y  %H:%M:%S.%f")
    
    print date
    

    Output would be:

    2015-11-19 18:45:00
    

    And you can access the desired values with:

    >>> date.year
    2015
    >>> date.month
    11
    >>> date.day
    19
    >>> date.hour
    18
    >>> date.minute
    45
    >>> date.second
    0
    

    You can check datetime's package documentation under section 8.1.7 for srtptime function's usage.

提交回复
热议问题