Advertisement
Xaric2
Dec 27th, 2022
58
0
Never
This is comment for paste CodefessReply-month
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. instead of all that unnecessary code just make it shorter with this
  2.  
  3. import calendar
  4.  
  5. def get_month(date_string):
  6.     # Split the date string into a list of three strings, representing the day, month, and year
  7.     try:
  8.         day, month, year = date_string.split("-")
  9.         # Convert the month string to an integer
  10.         month_number = int(month)
  11.     except (ValueError, TypeError):
  12.         # Handle invalid input
  13.         return None, None, None
  14.    
  15.     # Get the month name from the calendar module
  16.     month_name = calendar.month_name[month_number]
  17.    
  18.     return int(day), month_name, int(year)
  19.  
  20. # Test the function with the input string '26 - 01 - 2022'
  21. print(get_month("26-01-2022"))  # Output: (26, "January", 2022)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement