Unix Timestamps with Python

Unix timestamps are very easy to use in Python.

Current Unix timestamp (in seconds) with Python

             
            import time
            timestamp = int(time.time()) 
            print(timestamp)
            
        
Run this code online.

Date to Unix timestamp with Python

             
            from datetime import datetime
            import time
            datestring = '2025-10-21 10:11:15'
            dt = datetime.strptime(datestring, '%Y-%m-%d %H:%M:%S')
            timestamp = time.mktime(dt.timetuple())
            print(timestamp)
            
        
Run this code online.

Convert Unix timestamp to date with Python

            
            from datetime import datetime
            import time
                
            timestamp = 1691611275
            print(datetime.utcfromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S'))
            
        
Run this code online.