How to convert date in Python from 2021-01-11
as YYYY-MM-DD
to dd:mm:yyyy
like 11:01:2021
?
Asked
Active
Viewed 1.2k times
1

martineau
- 119,623
- 25
- 170
- 301
-
Does this answer your question? [Python: How to convert datetime format?](https://stackoverflow.com/questions/6288892/python-how-to-convert-datetime-format) – FObersteiner Jan 11 '21 at 06:33
3 Answers
4
You can do that in a very simple way with datetime:
import datetime
original_date = datetime.datetime.strptime("2021-01-11", '%Y-%m-%d')
formatted_date = original_date.strftime("%d:%m:%Y")

Marc Dillar
- 471
- 2
- 9
1
You can easily change strings to dates and back to string in any format thanks to datetime package.
from datetime import datetime
datetime.strptime("2021-01-11", "%Y-%m-%d").strftime("%d:%m:%Y")
## Output
'11:01:2021'

Hasan Salim Kanmaz
- 426
- 4
- 12
0
date = "2021-01-11"
lst_date = date.split("-")
new_date = lst_date[2]+":"+lst_date[1]+":"+lst_date[0]
Output:
11:01:2021

Synthase
- 5,849
- 2
- 12
- 34
-
An approach involving strptime/strftime should be preferred since it also validates the input. – FObersteiner Jan 11 '21 at 06:34