[Solved] Friendly Date Ranges-Returning correct array but without quotes

[Solved] Friendly Date Ranges-Returning correct array but without quotes
0

#1

Here is my code:

function makeFriendlyDates(arr) {
   var obj={}; 
   var string=arr.toString();
   var newstr=string.split(/[\s-,]+/);
      obj.year1=newstr[0];
      obj.month1=newstr[1];
      obj.day1=newstr[2];
      obj.year2=newstr[3];
      obj.month2=newstr[4];
      obj.day2=newstr[5];
   
   
 switch (obj.day1){
   case "01":
       obj.day1 = "1st";
       break;
   case "21":
   case "31":
       obj.day1 = obj.day1 + "st";
       break;
   case "02":
       obj.day1 = "2nd";
       break;
   case "22":
       obj.day1 = "22nd";
       break;
   case "03":
       obj.day1 = "3rd";
       break;
   case "23":
       obj.day1 = "23rd";
       break;
   case "04":
       obj.day1 = "4th";
       break;
   case "05":
       obj.day1 = "5th";
       break;
   case "06":
       obj.day1 = "6th";
       break;
   case "07":
       obj.day1 = "7th";
       break;
   case "08":
       obj.day1 = "8th";
       break;
   case "09":
       obj.day1 = "9th";
       break;
   default:
       obj.day1 = obj.day1 + "th";
 }
  
  switch (obj.day2){
   case "01":
       obj.day2 = "1st";
       break;
   case "21":
   case "31":
       obj.day2 = obj.day2 + "st";
       break;
   case "02":
       obj.day2 = "2nd";
       break;
   case "22":
       obj.day2 = "22nd";
       break;
   case "03":
       obj.day2 = "3rd";
       break;
   case "23":
       obj.day2 = "23rd";
       break;
   case "04":
       obj.day2 = "4th";
       break;
   case "05":
       obj.day2 = "5th";
       break;
   case "06":
       obj.day2 = "6th";
       break;
   case "07":
       obj.day2 = "7th";
       break;
   case "08":
       obj.day2 = "8th";
       break;
   case "09":
       obj.day2 = "9th";
       break;
   default:
       obj.day2 = obj.day2 + "th";
 }
  
  
 switch (obj.month1) {
    case "01":
        obj.month1 = "January";
        break;
    case "02":
        obj.month1 = "February";
        break;
    case "03":
        obj.month1 = "March";
        break;
    case "04":
        obj.month1 = "April";
        break;
    case "05":
        obj.month1 = "May";
        break;
    case "06":
        obj.month1 = "June";
        break;
    case "07":
        obj.month1 = "July";
        break;
    case "08":
        obj.month1 = "August";
        break;
    case "09":
        obj.month1 = "September";
        break;
    case "10":
        obj.month1 = "October";
        break;
    case "11":
        obj.month1 = "November";
        break;
    case "12":
        obj.month1 = "December";
       
 }
  
  switch (obj.month2) {
    case "01":
        obj.month2 = "January";
        break;
    case "02":
        obj.month2 = "February";
        break;
    case "03":
        obj.month2 = "March";
        break;
    case "04":
        obj.month2 = "April";
        break;
    case "05":
        obj.month2 = "May";
        break;
    case "06":
        obj.month2 = "June";
        break;
    case "07":
        obj.month2 = "July";
        break;
    case "08":
        obj.month2 = "August";
        break;
    case "09":
        obj.month2 = "September";
        break;
    case "10":
        obj.month2 = "October";
        break;
    case "11":
        obj.month2 = "November";
        break;
    case "12":
        obj.month2 = "December";
       
 }
  var first=[obj.month1 + " " + obj.day1 + "," + obj.year1];
  var second=[obj.month2 + " " + obj.day2 + "," + obj.year2];
  var oneyear=obj.month1 + " " + obj.day1 + " " + obj.year1 + "," + 
             obj.month2 + " " + obj.day2;
  var nomonth=[obj.month1 + " " + obj.day1 + "," + obj.day2];
  var noyear=obj.month1 + " " + obj.day1 + "," + obj.month2 + " " + obj.day2;
     if (obj.year1 <= obj.year2 && obj.month1 === obj.month2 && 
         obj.day1 > obj.day2){
       return noyear;
     } else if (obj.month1 === obj.month2 && obj.year1 === obj.year2 && 
                obj.day1 !== obj.day2){
       return nomonth;
     } else if (obj.year1===obj.year2 && obj.month1 !== obj.month2){
       return oneyear;
     } else {
       return first + second;
     }

}

makeFriendlyDates(['2016-07-01', '2016-07-04']);"

Currently this returns: [“July 1st,4th”].
It needs to return: [“July 1st”,“4th”].

How can I make this happen?

Thanks!


#2

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.


#3

On line #179 of your code you have:

var nomonth=[obj.month1 + " " + obj.day1 + "," + obj.day2];

It should be:

var nomonth=[obj.month1 + " " + obj.day1, obj.day2];

You were creating an array with one string which had a comma in it.

SUGGESTION:
You have four SWITCH statements in your code which contain duplicate code for two of them. You should create a function call convertDay for the ones which involve obj.day1 and obj.day2. That way the code is written once. The same goes for the other two SWITCH statements. Create a convertMonth for dealing with obj.month1 and obj.month2


#4

@BenGitter

Oh, ok. Thank you!


#5

@rmdawson71
Thank you!