1

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?

Bachalo
  • 6,965
  • 27
  • 95
  • 189
  • The datetime documentation includes all of this information. – Prune Apr 08 '20 at 16:52
  • Does this answer your question? [Python datetime formatting without zero-padding](https://stackoverflow.com/questions/9525944/python-datetime-formatting-without-zero-padding) – andreis11 Apr 08 '20 at 17:30
  • If at all possible, consider *not* doing this. the ISO 8601 YYYY-MM-DD format is standard and unambiguous and has numerous advantages over M/D/YY. Among other issues, "4/8/20" will be interpreted as April 8 in the US and as August 4 in the UK -- and, depending on the context might refer to the year 1920 rather than 2020. – Keith Thompson Apr 09 '20 at 01:04

2 Answers2

0

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"

Gabio
  • 9,126
  • 3
  • 12
  • 32
  • @jizhihaoSAMA are you sure? It works perfect on my machine (python3.7+): https://repl.it/repls/FreeLooseMoto – Gabio Apr 08 '20 at 17:03
  • @jizhihaoSAMA Right! In Windows it's a bit different. I've updated my solution. Thank you for the comment. – Gabio Apr 08 '20 at 17:24
0

try this

from datetime import date

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