Time Difference Calculator

  • Share this:

Code introduction


Calculates the time difference in seconds between two time strings in the format 'YYYY-MM-DD HH:MM:SS'.


Technology Stack : Time

Code Type : Function

Code Difficulty : Intermediate


                
                    
import os
import re
import sys
import time

def get_time_difference(start, end):
    """
    Calculate the difference in seconds between two time strings.

    Args:
    start (str): The start time in the format 'YYYY-MM-DD HH:MM:SS'.
    end (str): The end time in the format 'YYYY-MM-DD HH:MM:SS'.

    Returns:
    int: The difference in seconds between the two times.
    """
    return int(time.mktime(time.strptime(end, '%Y-%m-%d %H:%M:%S'))) - int(time.mktime(time.strptime(start, '%Y-%m-%d %H:%M:%S')))                
              
Tags: