Friendly Date Ranges

Friendly Date Ranges
0

#1

I’ve found converting the month and day for this exercise fairly straightforward. The big issue I’m running into is the logic behind if/when to print the year. I’ve written the following sudocode to break down the different scenarios:

``````-if dates are < 3 months apart && occurred recently --> no year is printed.
-if dates are < 12 months apart && occur in the future --> the starting year is printed. ending year not necessary.
-if date are > 12 months apart --> both start & end years are printed.
-if dates are the same day --> that full day's date is printed.
``````

Whoa. Where & how do I begin to implement this?! It seems to me I need to calculate the distance between the two dates and go from there. Is this correct or am I over complicating matters?

#2

I also had a few problems with this one & had to introduce a whole mess of checks.

([“2022-09-05”, “2023-09-05”]) would need to be date >= 12 months apart & you’d then need to factor in days for the other arguments such as
([“2022-09-05”, “2023-09-04”])

#3

That’s what I’m afraid of, these if/else if statements are going to start looking awful messy pretty quickly if that’s indeed what needs to be done here.

Also I’m not too confident how one deals with the math this implies. I mean looking at [“2022-09-05”, “2023-09-04”] a human can pretty quickly see that is one day short of a year. But how do you calculate that it’s 364 days so that fact is obvious to the computer?

#4

I imagine the exercise is meant to improve familiarity with javascripts Date().

When you pass a well-formatted date (yyyy-mm-dd) into Date, javascript provides you an object with a lot of functionality to make this sort of task easier.

Just an example, say you wanted to determine the difference between 2 dates:

``````var date1 = new Date('2016-07-01');
var date2 = new Date('2016-07-04');
var timeDifference = date2.getTime() - date1.getTime(); // = 259200000 (milliseconds)
alert('The time difference is: ' + timeDifference / 1000 / 60 / 60 / 24 + ' days!')
``````

Is a quick and easy use case.

#5

Ah this is very helpful, I did not know this was possible. Should make life a lot easier!