daily pastebin goal
25%
SHARE
TWEET

FCC Friendly Date Ranges Algorithm

a guest Aug 30th, 2016 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. function makeFriendlyDates(arr) {
  3.   var months = ["January", "Febraury", "March", "April",
  4.                "May", "June", "July", "August",
  5.                "September", "October", "November", "December"];
  6.  
  7.   function ordinal(num){
  8.     var end = "th";
  9.     if (num % 10 == 1){
  10.       end = "st";
  11.     }else if (num % 10 == 2){
  12.       end = "nd";
  13.     }else if (num % 10 == 3 && num != 13){
  14.       end = "rd";
  15.     }
  16.     return num + end;
  17.   }
  18.  
  19.   var result = [];
  20.   var one = arr[0].split('-');
  21.   var two = arr[1].split('-');
  22.   for (var i = 0; i<one.length; i++){
  23.     one[i] = parseInt(one[i]);
  24.   }
  25.   for (var j = 0; j<two.length; j++){
  26.     two[j] = parseInt(two[j]);
  27.   }
  28.  
  29.   //year, month, day
  30.  
  31.   var date1 = new Date(one[0], one[1] - 1, one[2]);
  32.   var date2 = new Date(two[0], two[1] - 1, two[2]);
  33.  
  34.   //used for testing
  35.   //date1 = new Date(2022, 9 - 1, 5);
  36.   //date2 = new Date(2023, 9 - 1, 4);
  37.  
  38.   var month2 = date2.getUTCFullYear()*12 + date2.getUTCMonth();
  39.   var month1 = date1.getUTCFullYear()*12 + date1.getUTCMonth();
  40.  
  41.   var dayDiff = Math.abs(date2.getUTCDate() - date1.getUTCDate());
  42.   var monthDiff = month2 - month1;
  43.   var yearDiff = date2.getUTCFullYear() - date1.getUTCFullYear();
  44.  
  45.   //same day
  46.   if (dayDiff === 0 && monthDiff === 0 && yearDiff === 0){
  47.     return [months[date1.getUTCMonth()] + " " + ordinal(date1.getUTCDate()) + ", " +
  48.       date1.getUTCFullYear()];
  49.   }else if (monthDiff === 0 && date1.getUTCFullYear() == date2.getUTCFullYear()){
  50.     //less than 12 months and same month and same year
  51.     result.push(months[date1.getUTCMonth()] + " " + ordinal(date1.getUTCDate()));
  52.     result.push(ordinal(date2.getUTCDate()));
  53.     return result;
  54.   }else if (monthDiff < 12 && date1.getUTCFullYear() == date2.getUTCFullYear()){
  55.     //less than 12 months and same year
  56.     result.push(months[date1.getUTCMonth()] + " " + ordinal(date1.getUTCDate()) + ", " +
  57.       date1.getUTCFullYear());
  58.     result.push(months[date2.getUTCMonth()] + " " + ordinal(date2.getUTCDate()));
  59.     return result;
  60.   }else if (monthDiff < 12){
  61.     //less than 12 months
  62.     result.push(months[date1.getUTCMonth()] + " " + ordinal(date1.getUTCDate()));
  63.     result.push(months[date2.getUTCMonth()] + " " + ordinal(date2.getUTCDate()));
  64.     return result;
  65.   }else if (yearDiff == 1 && monthDiff == 12 && date1.getUTCDate() > date2.getUTCDate()){
  66.     result.push(months[date1.getUTCMonth()] + " " + ordinal(date1.getUTCDate()) + ", " +
  67.       date1.getUTCFullYear());
  68.     result.push(months[date2.getUTCMonth()] + " " + ordinal(date2.getUTCDate()));
  69.     return result;
  70.   }else if (yearDiff >= 1){
  71.     result.push(months[date1.getUTCMonth()] + " " + ordinal(date1.getUTCDate()) + ", " +
  72.       date1.getUTCFullYear());
  73.     result.push(months[date2.getUTCMonth()] + " " + ordinal(date2.getUTCDate()) + ", " +
  74.       date2.getUTCFullYear());
  75.     return result;
  76.   }
  77.  
  78. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top