← Back to challenges

Get the Months Between Two Dates

JavaScriptHardarraysdatessorting

Instructions

Create a function that, given 2 dates, returns the names of the months that are present between them (inclusive).

Examples

Input

var january = new Date(2017, 0, 1);
var march = new Date(2017, 2, 1);

monthsInterval(january, march)

Output

['January', 'February', 'March']

Input

var december = new Date(2017, 11, 1);
var january = new Date(2018, 0, 1);

monthsInterval(december, january)

Output

['January', 'December']

Input

var january2017 = new Date(2017, 0, 1);
var january2018 = new Date(2018, 0, 1);

monthsInterval(january2017, january2018)

Output

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

(Notice that January is not duplicated!)

Notes

  • The returned array should include the months of dateStart and dateEnd (inclusive)
  • The returned array must not include duplicate values
  • The month names returned by the function should be sorted (not alphabetically, but ordered by which comes first (January = 1st month, February = 2nd month, … , December = 12th month))
  • The function should produce the same output even if dateStart is greater than dateEnd
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.