The Wayback Machine - https://web.archive.org/web/20240118172154/https://www.geeksforgeeks.org/ctime-function-of-datetime-date-class-in-python/
Open In App
Related Articles

ctime() Function Of Datetime.date Class In Python

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The ctime() function is used to return a string containing the date and time.

Syntax: ctime()

Parameters: This function does not accept any parameter.

Return values: This function returns a string containing the date and time.

The format of the string representation:

  • The string is of 24-character length.
  • Week is printed as a three-letter word.
Sun, Mo, Tue, Wed, Thu, Fri, Sat
  • Months are represented as three-digit letters
Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
  • The day of the month is represented as a two-digit number
  • Time is represented in HH:MM:SS format
  • The year is a 4 digit number

Example 1: Getting a string containing the date and time

Python3




# Python3 code to demonstrate
# Getting a string containing
# the date and time
  
# Importing datetime and time module 
import datetime
import time
  
# Calling the time() function
# to return current time
Todays_time = time.time()
  
# Printing today's time
print(Todays_time)
  
# Calling the fromtimestamp() function
# to get date from the current time
date_From_CurrentTime = datetime.date.fromtimestamp(Todays_time);
  
# Printing the current date
print("Date for Timestamp used is: %s"%date_From_CurrentTime);
  
# Calling the ctime() function over the above date
print("Today's date: %s"%date_From_CurrentTime.ctime());


Output:

1627282355.0111642
Date for Timestamp used is: 2021-07-26
Today's date: Mon Jul 26 00:00:00 2021

Example 2: Getting a string containing the date and time

Python3




# Python3 code to demonstrate
# Getting a string containing
# the date and time
  
# Importing datetime module 
import datetime
  
# Initializing a date object for a 
# different date
Date = datetime.date(2013, 2, 10);
  
# Calling the ctime() function 
# over the above date
print("The date: %s"%Date.ctime());


Output:

The date: Sun Feb 10 00:00:00 2013

Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.
Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 28 Jul, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials