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 7,365 times
0
martineau
- 112,593
- 23
- 157
- 280
POOJAN SHAH
- 19
- 1
- 3
-
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
3
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
- 423
- 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
- 422
- 3
- 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
- 4,920
- 2
- 9
- 31
-
An approach involving strptime/strftime should be preferred since it also validates the input. – FObersteiner Jan 11 '21 at 06:34