write python code to print time live
- User
write python code to print time live
Assistant
Here's Python code to print the current time live (updating continuously):
import time from datetime import datetime import sys # Clear line and print time continuously while True: current_time = datetime.now().strftime("%H:%M:%S") sys.stdout.write(f"\rCurrent time: {current_time}") sys.stdout.flush() time.sleep(1)If you want a more advanced version with date and milliseconds:
import time from datetime import datetime import os # Clear screen based on OS def clear_screen(): os.system('cls' if os.name == 'nt' else 'clear') # Print time with more details while True: clear_screen() now = datetime.now() print(f"Date: {now.strftime('%Y-%m-%d')}") print(f"Time: {now.strftime('%H:%M:%S.%f')[:-3]}") # Include milliseconds print("\nPress Ctrl+C to stop") time.sleep(0.1) # Update every 100ms
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.