Please help guys! Friendly Date Ranges Feedback Please?!

Please help guys! Friendly Date Ranges Feedback Please?!
0

#1

The following is my code… I feel it is cumbersome but it works. I know I can make vast improvements such as creating functions or cases to take care of certain things but what do you guys think? Is it okay as my first solution for it? Bad practice? Any feedback would help greatly, thanks.

function makeFriendlyDates(arr) {

  function yearOrNot(arr) {
    var diff =  Math.floor(( Date.parse(arr[1]) - Date.parse(arr[0]) ) / 86400000);
    return diff;
  }

  var myStartArray = arr[0].split("-");
  var myEndArray = arr[1].split("-");
  
  var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  
  var days = ["1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th", "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", "20th", "21st", "22nd", "23rd", "24th", "25th", "26th", "27th", "28th", "29th", "30th", "31st"];
  
  var returnArray = [];
  
   if (parseInt(myStartArray[0], 10) === 2016 && parseInt(myEndArray[0], 10) === 2016) {
    returnArray[0] = months[parseInt(myStartArray[1], 10) - 1] + " " + days[parseInt(myStartArray[2], 10) - 1]; 
    
    returnArray[1] = days[parseInt(myEndArray[2], 10) - 1];
  }
   else if (yearOrNot(arr) === 365 && myStartArray !== myEndArray) {
    returnArray[0] = months[parseInt(myStartArray[1], 10) - 1] + " " + days[parseInt(myStartArray[2], 10) - 1] + ", " + myStartArray[0]; 
    
    returnArray[1] = months[parseInt(myEndArray[1], 10) - 1] + " " + days[parseInt(myEndArray[2], 10) - 1] + ", " + myEndArray[0];
   }
   else if (arr[0] === arr[1]) {
     returnArray[0] = months[parseInt(myStartArray[1], 10) - 1] + " " + days[parseInt(myStartArray[2], 10) - 1] + ", " + myStartArray[0];
   }
   else if (yearOrNot(arr) < 365 && parseInt(myStartArray[0], 10) === 2016) {
     returnArray[0] = months[parseInt(myStartArray[1], 10) - 1] + " " + days[parseInt(myStartArray[2], 10) - 1]; 
    
     returnArray[1] = months[parseInt(myEndArray[1], 10) - 1] + " " + days[parseInt(myEndArray[2], 10) - 1];
   }
  else if (yearOrNot(arr) > 365) {
    returnArray[0] = months[parseInt(myStartArray[1], 10) - 1] + " " + days[parseInt(myStartArray[2], 10) - 1] + ", " + myStartArray[0]; 
  
    returnArray[1] = months[parseInt(myEndArray[1], 10) - 1] + " " + days[parseInt(myEndArray[2], 10) - 1] + ", " + myEndArray[0];
  }
  else if (myStartArray[1] === myEndArray[1] && myStartArray !== myEndArray) {
    returnArray[0] = months[parseInt(myStartArray[1], 10) - 1] + " " + days[parseInt(myStartArray[2], 10) - 1] + ", " + myStartArray[0]; 
    
    returnArray[1] = months[parseInt(myEndArray[1], 10) - 1] + " " + days[parseInt(myEndArray[2], 10) - 1];
  }
  else if (yearOrNot(arr) < 365 && myStartArray !== myEndArray) {
    returnArray[0] = months[parseInt(myStartArray[1], 10) - 1] + " " + days[parseInt(myStartArray[2], 10) - 1] + ", " + myStartArray[0]; 
    
    returnArray[1] = months[parseInt(myEndArray[1], 10) - 1] + " " + days[parseInt(myEndArray[2], 10) - 1];
  }
  return returnArray;
} 

makeFriendlyDates(["2022-09-05", "2023-09-05"]);

#2

I usually use the Moments.js library when I need to work with dates. They Moments.js allows you to Parse, validate, manipulate, and display dates in JavaScript.