I have Date as 20211115 (YYYYMMDD), need to convert this to 11-Nov-2021 in python.
Please let me know the suggestions/answers for this.
Asked
Active
Viewed 93 times
1
Harsha Biyani
- 6,633
- 9
- 33
- 55
Shanthi K
- 7
- 5
-
Welcome back to Stack Overflow. Please keep in mind that [you are expected to do research before asking](https://meta.stackoverflow.com/questions/261592). It is very easy to answer this question using any search engine. I can [literally copy and paste your question title](https://duckduckgo.com/?q=How+to+Convert+Date+20211115+to+15-Nov-2021+in+python) and get good answers. – Karl Knechtel Dec 31 '21 at 06:34
2 Answers
2
I assume that your date in YYYYmmdd format
from datetime import datetime
date_string = "20211115"
date_object = datetime.strptime(date_string, "%Y%m%d")
date_time = date_object.strftime("%d-%b-%Y")
print("date:",date_time)
output : 15-Nov-2021
Dharman
- 26,923
- 21
- 73
- 125
Akash senta
- 445
- 5
- 15
-
-
Great. Please accept answer as correct. So it helps others as well. – Akash senta Dec 31 '21 at 06:07
1
The key is strftime()
timestamp = 1528797322
date_time = datetime.fromtimestamp(timestamp)
print("Date time object:", date_time)
d = date_time.strftime("%m/%d/%Y, %H:%M:%S")
print("Output 2:", d)
d = date_time.strftime("%d %b, %Y")
print("Output 3:", d)
d = date_time.strftime("%d %B, %Y")
print("Output 4:", d)
d = date_time.strftime("%I%p")
print("Output 5:", d)
When you run the program, the output will be:
Date time object: 2018-06-12 09:55:22
Output 2: 06/12/2018, 09:55:22
Output 3: 12 Jun, 2018
Output 4: 12 June, 2018
Output 5: 09AM
%b Abbreviated month name -> Jan, Feb, ..., Dec
%B Full month name. January, February,...
Gürkan Özdem
- 109
- 7