How to convert a time string to seconds?

前端 未结 9 1878
轮回少年
轮回少年 2020-11-30 00:44

I need to convert time value strings given in the following format to seconds, for example:

1.\'00:00:00,000\' -> 0 seconds

2.\'00:00:10,000\' -> 10 s         


        
9条回答
  •  生来不讨喜
    2020-11-30 00:49

    import time
    from datetime import datetime
    
    t1 = datetime.now().replace(microsecond=0)
    time.sleep(3)
    now = datetime.now().replace(microsecond=0)
    print((now - t1).total_seconds())
    

    result: 3.0

提交回复
热议问题