python: difference of two timedate strings

前端 未结 4 641
情深已故
情深已故 2021-01-13 11:53

I have two date strings (taken from user input and can vary greatly)

s1 = \'2011:10:01:10:30:00\'
s2 = \'2011:10:01:11:15:00\'

I wish to fi

4条回答
  •  渐次进展
    2021-01-13 12:41

    The time module can be helpful for this.

    import time
    
    s1 = '2011:10:01:10:30:00'
    s2 = '2011:10:01:11:15:00'
    
    s1Time = time.strptime(s1, "%Y:%m:%d:%H:%M:%S")
    s2Time = time.strptime(s2, "%Y:%m:%d:%H:%M:%S")
    
    deltaInMinutes = (time.mktime(s2Time) - time.mktime(s1Time)) / 60.0
    print deltaInMinutes, "minutes"
    

提交回复
热议问题