要将 date 类型的变量 today 转换为 string,可以使用 strftime() 方法来实现。strftime() 方法可以将日期格式化为特定的字符串格式。

下面是一个示例代码:

import datetime

today = datetime.date.today()
today_string = today.strftime("%Y-%m-%d")
print(today_string)

输出结果会是类似 "2022-01-01" 的字符串,表示当前日期的年、月、日信息。在 strftime() 方法中,"%Y-%m-%d" 是日期的格式化字符串,其中 "%Y" 表示四位数的年份,"%m" 表示两位数的月份,"%d" 表示两位数的日期。

你可以根据需求调整 strftime() 方法中的格式化字符串,以满足你想要的日期格式。

用 python 将 date 类型的变量 today 转换为 string

原文地址: https://www.cveoy.top/t/topic/hJ60 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录