Skip to content

Instantly share code, notes, and snippets.

@Berkays
Created July 14, 2022 19:49
Show Gist options
  • Save Berkays/de5f36bf1585facca17902b37c983551 to your computer and use it in GitHub Desktop.
Save Berkays/de5f36bf1585facca17902b37c983551 to your computer and use it in GitHub Desktop.
Frontend Assessment
/*
* 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'],
];
const dayCounts = [
31,
28,
31,
30,
31,
30,
31,
31,
30,
31,
30,
31
];
// Receive string of dates one after each other
function outputDate(dates) {
let leapDayCount = 0;
let split = dates[0].split('.');
const startDate = {
day: parseInt(split[0]),
month: parseInt(split[1]),
year: parseInt(split[2])
};
split = dates[1].split('.');
const endDate = {
day: parseInt(split[0]),
month: parseInt(split[1]),
year: parseInt(split[2])
};
let i = startDate.year;
// Skip starting year leap day check
if (startDate.month > 2)
i = startDate.year + 1;
for (; i <= endDate.year; i++) {
if (isLeapYear(i))
leapDayCount++;
}
// Discard extra leap year if month is less than 2 (Feb)
if (isLeapYear(endDate.year) && endDate.month <= 2)
leapDayCount--;
let totalDays = (endDate.year - startDate.year) * 365;
totalDays += leapDayCount;
totalDays = totalDays - getCumulativeDayCount(startDate.day, startDate.month);
totalDays = totalDays + getCumulativeDayCount(endDate.day, endDate.month);
const totalYears = parseInt(totalDays / 365);
const totalMonths = parseInt((totalDays % 365) / 30);
let yearString = '';
let monthString = '';
if (totalYears == 1)
yearString = `${totalYears} year, `;
else if (totalYears > 1)
yearString = `${totalYears} years, `;
if (totalDays <= 30)
monthString = '';
else if (totalMonths == 1)
monthString = `${totalMonths} month, `;
else if (totalMonths > 1)
monthString = `${totalMonths} months, `;
return `${yearString}${monthString}total ${totalDays} days`;
}
// Check leap year
function isLeapYear(year) {
if ((year % 4 == 0 && year % 100 > 0) || (year % 400 == 0))
return true;
return false;
}
// Get day count upto specified month and day of a year.
function getCumulativeDayCount(day, month) {
let count = 0;
for (let i = 1; i < month; i++)
count += dayCounts[i - 1];
count += day;
return count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment