从年月日更改日期格式

时间:2020-04-08 16:48:37

标签: python date

使用以下内容计算今天的日期。 导入日期时间

today = datetime.date.today()
print(today)

它以 2020-04-08 的格式返回日期。 但是我需要 4/8/20

格式

我该怎么做?

2 个答案:

答案 0 :(得分:0)

使用-删除日期和月份中的前导零:

from datetime import date

d = date.today().strftime("%-m/%-d/%y")
print(d)

如果使用Windows,请将格式字符串更改为"%#m/%#d/%y"

答案 1 :(得分:0)

尝试

from datetime import date

today = date.today().strftime("%m/%d/%y")
print(today)