Skip to content

Instantly share code, notes, and snippets.

@planta
Created November 2, 2022 11:45
Show Gist options
  • Save planta/ce23901bc0154103c1289d65423ebd3b to your computer and use it in GitHub Desktop.
Save planta/ce23901bc0154103c1289d65423ebd3b to your computer and use it in GitHub Desktop.
/*
* 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 monthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
function isLeapYear(year) {
// check if year is leap year
return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}
function getNumberOfYears(startDate, endDate) {
const startDay = parseInt(startDate[0]), startMonth = parseInt(startDate[1]), startYear = parseInt(startDate[2])
const endDay = parseInt(endDate[0]), endMonth = parseInt(endDate[1]), endYear = parseInt(endDate[2])
// calculate number of years
var years = endYear - startYear
// check if starting month is higher than ending month
// or if month is the same but starting day is higher than ending day
// and subtract 1 year in those 2 cases because time between those dates
// is less than 1 full year in those cases
if((startMonth > endMonth) || (startMonth == endMonth && startDay > endDay)) {
years--
}
return years
}
function getNumberOfMonths(startDate, endDate) {
const startDay = parseInt(startDate[0]), startMonth = parseInt(startDate[1]), startYear = parseInt(startDate[2])
const endDay = parseInt(endDate[0]), endMonth = parseInt(endDate[1]), endYear = parseInt(endDate[2])
var months = 0
// calculate number of months
if(startYear == endYear) {
months = endMonth - startMonth
} else {
if(startMonth < endMonth) {
months = endMonth - startMonth
} else if(startMonth > endMonth) {
months = (12 - startMonth) + endMonth
} else {
if(startDay > endDay) {
months = 12
}
}
}
// if starting day is higher than ending day then subtract one month from total
if(startDay > endDay) {
months--
}
return months
}
function getNumberOfDays(startDate, endDate) {
const startDay = parseInt(startDate[0]), startMonth = parseInt(startDate[1]), startYear = parseInt(startDate[2])
const endDay = parseInt(endDate[0]), endMonth = parseInt(endDate[1]), endYear = parseInt(endDate[2])
var days = 0
for(var currentYear = startYear; currentYear <= endYear; currentYear++) {
// check if current year is start year, end year or in between
if(currentYear == startYear) {
var currentYearEndMonth = 12
if(currentYear == endYear) {
currentYearEndMonth = endMonth
}
for(var currentMonth = startMonth; currentMonth <= currentYearEndMonth; currentMonth++) {
if(startMonth == endMonth && startYear == endYear) {
days += endDay - startDay
} else if(currentMonth == startMonth) {
if(currentMonth == 2 && isLeapYear(currentYear)) {
days += 29 - startDay
} else {
days += monthDays[currentMonth-1] - startDay
}
} else if(currentMonth == endMonth && startYear == endYear) {
days += endDay
} else {
days += monthDays[currentMonth-1]
if(currentMonth == 2 && isLeapYear(currentYear)) {
days++
}
}
}
} else if(currentYear == endYear) {
var currentYearEndMonth = 12
if(currentYear == endYear) {
currentYearEndMonth = endMonth
}
for(var currentMonth = 1; currentMonth <= endMonth; currentMonth++) {
if(currentMonth == endMonth) {
days += endDay
} else {
days += monthDays[currentMonth-1]
if(currentMonth == 2 && isLeapYear(currentYear)) {
days++
}
}
}
} else {
// currentYear is in between starting and ending year
// so we add all of the days within the year
days += 365
// add one additional day in case of leap year
if(isLeapYear(currentYear)) {
days++
}
}
}
return days
}
// Receive string of dates one after each other
function outputDate(dates) {
var returnString = ''
const startDate = dates[0].split('.')
const endDate = dates[1].split('.')
const years = getNumberOfYears(startDate, endDate)
const months = getNumberOfMonths(startDate, endDate)
const days = getNumberOfDays(startDate, endDate)
// create year part of return string
if(years > 0) {
if(years == 1) {
returnString += years + ' year, '
} else {
returnString += years + ' years, '
}
}
// create month part of return string
if(months > 0) {
if(months == 1) {
returnString += months + ' month, '
} else {
returnString += months + ' months, '
}
}
// create total days part of return string
if(days == 1) {
returnString += 'total ' + days + ' day'
} else {
returnString += 'total ' + days + ' days'
}
return returnString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment