Ctime() Function Of Datetime.date Class In Python

Function Name:

            ctime

Function Signature:

ctime()

Return Value:

Returns a string that contains the date and time.

Function Overview:

  • For a Python datetime.date object the ctime() method returns the string representation of the date value and time.
  • Since date class does not have any time related attributes the string will have its time component as 00:00:00.
  • The format of the string representation:
    • The string is of 24-character length.
    • The day of the 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
    • Year is a 4 digit number

Function type:

            Instance method

Example:

import datetime

import time

 

# Construct a date object for today

todaysDate = datetime.date.fromtimestamp(time.time());

 

# Print the ctime

print("Today's date:%s"%todaysDate.ctime());

 

# Construct a date object for a future date

futureDate = datetime.date(2020, 1, 31);

 

# Print the ctime

print("A date in future:%s"%futureDate.ctime());

print("Length of the date string:%s"%len(futureDate.ctime()));

Output:

Today's date:Fri Jul 12 00:00:00 2019

A date in future:Fri Jan 31 00:00:00 2020

Length of the date string:24


Copyright 2024 © pythontic.com