-1

How can I get the the import function to convert the date into a European date ? Example: (‘4/7/14’) returns ‘07/04/12’

import datetime

today = datetime.date.today()
print(today)
Johhhbb
  • 1
  • 1
  • 4

1 Answers1

0

that is what datetime.strftime is for. have a look at the format codes to get your desired result.

for example:

import datetime

today = datetime.date.today()

print(today.strftime("%Y-%m-%d"))     # 2020-12-02
print(today.strftime("%a %d %b %Y"))  # Wed 02 Dec 2020
print(today.strftime("%d/%m/%Y"))     # 02/12/2020
hiro protagonist
  • 44,693
  • 14
  • 86
  • 111
  • How could I use the return function to do it instead of the print function? – Johhhbb Dec 02 '20 at 16:37
  • `return` is something you can only use inside a function. `strftime` returns a string; you could always assign it to a name: `today_str = today.strftime("%d/%m/%Y")`. or what do you mean? – hiro protagonist Dec 02 '20 at 17:23
  • Once I complete the function, I would want it to return the converted date by passing a parameter. Like for instance def european(date): then at the end like return European_date which would give me a converted value – Johhhbb Dec 02 '20 at 17:33
  • `def european(date): return today.strftime("%Y-%m-%d")`. – hiro protagonist Dec 02 '20 at 17:42
  • Any suggestions for building the function so it runs smoothly? I’m a bit confused – Johhhbb Dec 02 '20 at 17:47
  • i have no idea what you really want, sorry. maybe you should describe that precisely in a fresh question and post the attempts you made to solve your problem.(how does the suggested function not run 'smoothly'?) – hiro protagonist Dec 02 '20 at 18:22