1

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.

Harsha Biyani
  • 7,049
  • 9
  • 37
  • 61
Shanthi K
  • 5
  • 6
  • 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 Answers2

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
  • 30,962
  • 25
  • 85
  • 135
Akash senta
  • 483
  • 7
  • 16
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,...

Referance