简体   繁体   中英

Changing date format from year month day

Using the following to calculate today's date. import datetime

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

It returns the date in format of 2020-04-08 However I need it in the format of 4/8/20

How might I accomplish this?

Use - to remove the leading zeros from day and month:

from datetime import date

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

If you use Windows, change the format string to "%#m/%#d/%y"

try this

from datetime import date

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM