PythonDatetime.date类的ctime()函数
ctime()函数用于返回包含日期和时间的字符串。
Syntax: ctime()
Parameters: This function does not accept any parameter.
Return values: This function returns a string containing the date and time.
字符串表示的格式:
- 该字符串的长度为 24 个字符。
- Week 打印为三个字母的单词。
Sun, Mo, Tue, Wed, Thu, Fri, Sat
- 月份用三位数的字母表示
Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
- 月份中的日期表示为两位数
- 时间以 HH:MM:SS 格式表示
- 年份是一个 4 位数字
示例 1:获取包含日期和时间的字符串
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());
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());
输出:
1627282355.0111642
Date for Timestamp used is: 2021-07-26
Today's date: Mon Jul 26 00:00:00 2021
示例 2:获取包含日期和时间的字符串
蟒蛇3
# 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());
输出:
The date: Sun Feb 10 00:00:00 2013