Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how to write time in python

+21 votes
asked Jan 7 by Diganth Ramesh Urs (220 points)

3 Answers

+1 vote
answered Jan 9 by Peter Minarik (88,520 points)
0 votes
answered Jan 18 by Mohd Zia (190 points)
import time

0 votes
answered Jan 25 by ram thanish (140 points)

Here's the full code, incorporating explanations and a visual representation:

import time

# Get the current time as a timestamp (seconds since epoch)
current_time_seconds = time.time()
print("Timestamp (seconds since epoch):", current_time_seconds)

# Get the current time in a readable format
current_time_string = time.ctime(current_time_seconds)
print("Readable format:", current_time_string)

# Format the time using strftime()
formatted_time = time.strftime("%Y-%m-%d %H:%M:%S")  # Format: YYYY-MM-DD HH:MM:SS
print("Formatted time:", formatted_time)

# Get local time and UTC time
local_time = time.localtime()
print("Local time:", time.strftime("%Y-%m-%d %H:%M:%S", local_time))

utc_time = time.gmtime()
print("UTC time:", time.strftime("%Y-%m-%d %H:%M:%S", utc_time))

# Pause execution for 5 seconds

Key points:

  • The time module provides functions for working with time in Python.
  • time.time() returns the current time as a timestamp (seconds since epoch).
  • time.ctime() converts a timestamp to a readable string format.
  • time.strftime() formats time according to specified format codes.
  • time.localtime() and time.gmtime() return local time and UTC time, respectively.
  • time.sleep() pauses execution for a specified number of seconds.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.