Created
February 3, 2022 12:21
-
-
Save dms120/d945eef493f8557afb1c96398605cb99 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Your program must print string with the number of years and months and the total number of days between the dates. | |
* Dates are provided in dd.mm.yyyy format. | |
* You are not allowed to plug in JS libraries such as moment.js or date-fns directly into the code. All code need to be written in this file. | |
* | |
* Result must be shown as a string in years, months and total days. If years or months are 0, then it should not be displayed in the output. | |
* | |
* Example: | |
* Input: ['01.01.2000', '01.01.2016'] | |
* Output: | |
* '16 years, total 5844 days' | |
* | |
* Example 2: | |
* Input: ['01.11.2015', '01.02.2017'] | |
* | |
* Output: | |
* '1 year, 3 months, total 458 days' | |
*/ | |
const dates = [ | |
['01.01.2000', '01.01.2016'], | |
['01.01.2016', '01.08.2016'], | |
['01.11.2015', '01.02.2017'], | |
['17.12.2016', '16.01.2017'], | |
['01.01.2016', '01.01.2016'], | |
['28.02.2015', '13.04.2018'], | |
['28.01.2015', '28.02.2015'], | |
['17.03.2022', '17.03.2023'], | |
['17.02.2024', '17.02.2025'], | |
]; | |
// Receive string of dates one after each other | |
function outputDate(dates) { | |
var splitD1 = dates[0].split('.'); | |
var splitD2 = dates[1].split('.'); | |
// initialze dates with (year, month, day) - Month index starts in 0 (January) | |
var date1 = new Date(splitD1[2], splitD1[1] - 1, splitD1[0]); | |
var date2 = new Date(splitD2[2], splitD2[1] - 1, splitD2[0]); | |
// Days and Months will be calculated in separate | |
// it would be really hard to calculate one from the other because of the leap years | |
var days = daysDifference(date1, date2); | |
var totalMonths = monthsDifference(date1, date2); | |
var months = totalMonths % 12; | |
var years = (totalMonths - months) / 12; | |
// If the day of the month on D1 is higher than the day on D2 it's not a full month difference | |
if (months > 0 && date1.getDate() > date2.getDate()) { | |
months--; | |
} | |
var result = []; | |
if (years > 0) { | |
result.push(`${years} year${years > 1 ? "s" : ""}`) | |
} | |
if (months > 0) { | |
result.push(`${months} month${months > 1 ? "s" : ""}`) | |
} | |
result.push("total " + days + " days"); | |
return result.join(", "); | |
} | |
function monthsDifference(date1, date2) { | |
var months = (date2.getFullYear() - date1.getFullYear()) * 12; | |
return months + (date2.getMonth() - date1.getMonth()); | |
} | |
function daysDifference(date1, date2) { | |
return Math.ceil((date2.getTime() - date1.getTime()) / 86400000); // Round up the result (ignore time) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment