I have been trying to work this challenge for a while. It is finally working with a past date, it gives me the format that I want (year, month, days), but is not working with future dates. How can I rework this example, so it will also work with future dates? as of now I get an empty string.
function reworkedInBetweenDays(year, month, day) { var today = new Date(); var fromdate = new Date(year, month - 1, day); var yearsDiff = today.getFullYear() - fromdate.getFullYear(); var monthsDiff = today.getMonth() - fromdate.getMonth(); var daysDiff = today.getDate() - fromdate.getDate(); if (monthsDiff < 0 || (monthsDiff === 0 && daysDiff < 0)) yearsDiff--; if (monthsDiff < 0) monthsDiff += 12; if (daysDiff < 0) { var fromDateAux = fromdate.getDate(); fromdate.setMonth(fromdate.getMonth() + 1, 0); daysDiff = fromdate.getDate() - fromDateAux + today.getDate(); monthsDiff--; } var result = []; if (yearsDiff > 0) result.push(yearsDiff + (yearsDiff > 1 ? " years" : " year")); if (monthsDiff > 0) result.push(monthsDiff + (monthsDiff > 1 ? " months" : " month")); if (daysDiff > 0) result.push(daysDiff + (daysDiff > 1 ? " days" : " day")); return result.join(', '); } console.log(reworkedInBetweenDays(2013, 4, 8)); console.log(reworkedInBetweenDays(2014, 1, 16)); console.log(reworkedInBetweenDays(2016, 1, 31)); console.log(reworkedInBetweenDays(2017, 2, 16));
You just need to use Math.abs()
to getting your yearsDiff
function reworkedInBetweenDays(year, month, day) {
var today = new Date();
var fromdate = new Date(year, month - 1, day);
var yearsDiff = Math.abs(today.getFullYear() - fromdate.getFullYear()); //HERE
var monthsDiff = today.getMonth() - fromdate.getMonth();
var daysDiff = today.getDate() - fromdate.getDate();
if (monthsDiff < 0 || (monthsDiff === 0 && daysDiff < 0))
yearsDiff--;
if (monthsDiff < 0)
monthsDiff += 12;
if (daysDiff < 0) {
var fromDateAux = fromdate.getDate();
fromdate.setMonth(fromdate.getMonth() + 1, 0);
daysDiff = fromdate.getDate() - fromDateAux + today.getDate();
monthsDiff--;
}
var result = [];
if (yearsDiff > 0)
result.push(yearsDiff + (yearsDiff > 1 ? " years" : " year"));
if (monthsDiff > 0)
result.push(monthsDiff + (monthsDiff > 1 ? " months" : " month"));
if (daysDiff > 0)
result.push(daysDiff + (daysDiff > 1 ? " days" : " day"));
return result.join(', ');
}
console.log(reworkedInBetweenDays(2013, 4, 8));
console.log(reworkedInBetweenDays(2014, 1, 16));
console.log(reworkedInBetweenDays(2016, 1, 31));
console.log(reworkedInBetweenDays(2017, 2, 16));
It works adding this code at the end
if (yearsDiff < 0)
result.push(yearsDiff*(-1) + (yearsDiff*(-1) > 1 ? " years in the future" : " year in the future"));
if (monthsDiff < 0)
result.push(monthsDiff*(-1) + (monthsDiff*(-1) > 1 ? " months in the future" : " month in the future"));
if (daysDiff < 0)
result.push(daysDiff*(-1) + (daysDiff*(-1) > 1 ? " days in the future" : " day in the future"));
monthsDiff
and daysDiff
are both zero and yearsDiff
is -1, meaning none of them set off the if statements at the bottom. You need to add some code to handle when all these values are zero or negative.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.