-1

I have a list of date with this format : "190802 (2 august2019)". I would like to format it to "02-08-19" using datetime. Is there a fucntion to do?

Thanks

Below the Radar
  • 7,321
  • 11
  • 63
  • 142
user17241
  • 307
  • 1
  • 4
  • 16
  • 4
    Possible duplicate of [Converting string into datetime](https://stackoverflow.com/questions/466345/converting-string-into-datetime) – Poojan Oct 02 '19 at 12:48
  • Please post expected output of given sample along with what you have tried – Chris Oct 02 '19 at 12:49
  • `from datetime import datetime;d = datetime.strptime('190802', '%y%m%')` Will this work? Post your input and expected output – Poojan Oct 02 '19 at 12:52
  • @Chris , thanks; The expected format is 02-08-19 – user17241 Oct 02 '19 at 12:54

1 Answers1

1

If I understand your question, you have a list of string inputs and you want to format them.

from datetime import datetime

input_list = ["190802  (2 august2019)"]

for input in input_list:
    date = datetime.strptime(input[:6], "%y%m%d")
    formated_output = date.strftime("%d-%m-%y")
    print(formated_output)

>>>'02-08-19'
Below the Radar
  • 7,321
  • 11
  • 63
  • 142