返回未来的 Javascript(年、月、日)

Return future Javascript (year, month, day)

本文关键字:未来 Javascript 返回      更新时间:2023-09-26

我一直在尝试这个挑战。它终于可以使用过去的日期,它给了我想要的格式(年、月、日(,但不适用于未来的日期。如何重新设计此示例,使其也适用于将来的日期?截至目前,我得到一个空字符串。

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));

你只需要使用Math.abs()来获取你的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));

末尾添加此代码可以工作

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"));

monthsDiffdaysDiff 都是零,yearsDiff 是 -1,这意味着它们都没有触发底部的 if 语句。当所有这些值为零或负数时,您需要添加一些代码来处理。