These are chat archives for camperbot/bonfire-friendly-date-ranges

24th
Jan 2016
Maciej
@ketus
Jan 24 2016 15:59
hint
CamperBot
@camperbot
Jan 24 2016 15:59

hint [1/7]

## :construction: After this are possible spoiler hints.

Make sure you've tried to hard to solve it yourself before proceeding. :construction:

type hint for next hint :pencil: [Contribute at the FCC Wiki]

Maciej
@ketus
Jan 24 2016 15:59
hint
CamperBot
@camperbot
Jan 24 2016 15:59

hint [2/7]

# Problem Explanation:

  • Create a program that will take two dates and convert them into a more easy to understand date such as January 1st, 2017. It will also check the difference between them, and handles cases with no difference, more than a day, more than a month, more than a year, and more than a month and less than a year respectively.

type hint for next hint :pencil: [Contribute at the FCC Wiki]

Maciej
@ketus
Jan 24 2016 15:59
hint
CamperBot
@camperbot
Jan 24 2016 15:59

hint [3/7]

Hint: 1

  • Split the string into an array where you get the "YYYY", "MM", "DD"

type hint for next hint :pencil: [Contribute at the FCC Wiki]

Maciej
@ketus
Jan 24 2016 15:59
hint
CamperBot
@camperbot
Jan 24 2016 15:59

hint [4/7]

Hint: 2

  • You need to handle the case for "st", "nd", and "th". Note that 13 is "th" not "rd".

type hint for next hint :pencil: [Contribute at the FCC Wiki]

Maciej
@ketus
Jan 24 2016 15:59
hint
CamperBot
@camperbot
Jan 24 2016 15:59

hint [5/7]

Hint: 3

  • If you are using Date() to create instances of dates to work with, then use UTC time to avoid errors due to time zone difference between servers.

type hint for next hint :pencil: [Contribute at the FCC Wiki]

Maciej
@ketus
Jan 24 2016 15:59
hint
CamperBot
@camperbot
Jan 24 2016 15:59

hint [6/7]

Spoiler Alert!

687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif

Solution ahead!

type hint for next hint :pencil: [Contribute at the FCC Wiki]

Maciej
@ketus
Jan 24 2016 15:59
hint
CamperBot
@camperbot
Jan 24 2016 15:59

hint [7/7]

Code Solution:

function friendly(str) {

  var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

  // COnverst a YYYY-MM-DD string into a date object.
  function convertDate(str) {
    // Split the dates to work independently.
    var dateStr = str.split('-');

    // Force the dates into Universal time to avoid issues due to timezones.
    return (new Date(Date.UTC(dateStr[0], dateStr[1] - 1, dateStr[2])));

  }

  // Handles the case of the day's endings.
  function dateEnding(val) {
    switch (val) {
      case 1:
      case 21:
      case 31:
        return val + 'st';
      case 2:
      case 22:
        return val + 'nd';
      case 3:
      case 23:
        return val + 'rd';
      default:
        return val + 'th';
    }
  }

  // Checks for the real difference in months to avoid errors
  function monthDiff(date1, date2) {
    var month2 = date2.getUTCFullYear() * 12 + date2.getUTCMonth();
    var month1 = date1.getUTCFullYear() * 12 + date1.getUTCMonth();
    return month2 - month1;
  }

  // Get's the right month string.
  function getMonth(date) {
    return months[date.getUTCMonth()];
  }

  function displayDate() {

    // Handles same day
    if (date2.getTime() - date1.getTime() === 0) {
      return [getMonth(date1) + ' ' + dateEnding(date1.getUTCDate()) + ', ' + date1.getUTCFullYear()];
    }

    // Handles same month
    if (date1.getUTCMonth() === date2.getUTCMonth() && date1.getUTCFullYear() === date2.getUTCFullYear()) {
      return [getMonth(date1) + ' ' + dateEnding(date1.getUTCDate()), dateEnding(date2.getUTCDate())];
    }

    // Handles more than a month of difference, but less than 12 months
    if (monthDiff(date1, date2) < 12) {
      return [getMonth(date1) + ' ' + dateEnding(date1.getUTCDate()), getMonth(date2) + ' ' + dateEnding(date2.getUTCDate())];
    }

    // Handles cases with more than 12 months apaprt.
    return [getMonth(date1) + ' ' + dateEnding(date1.getUTCDate()) + ', ' + date1.getUTCFullYear(), getMonth(date2) + ' ' + dateEnding(date2.getUTCDate()) + ', ' + date2.getUTCFullYear()];
  }

  var date1 = convertDate(str[0]);
  var date2 = convertDate(str[1]);

  return displayDate();

}

Code Explanation:

  • Read comments in code

Credits:

If you found this page useful, you can give thanks by copying and pasting this on the main chat: thanks @Rafase282 @guyjoseph for your help with Bonfire: Friendly Date Ranges

NOTE: Please add your username only if you have added any relevant main contents to the wiki page. (Please don't remove any existing usernames.)

type hint for next hint :pencil: [Contribute at the FCC Wiki]