简体   繁体   English

返回未来日期 - Javascript(年,月,日)

[英]Return future date - Javascript (year, month, day)

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 你只需要使用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));

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. monthsDiffdaysDiff都是零, yearsDiff是-1,这意味着它们都没有在底部引发if语句。 You need to add some code to handle when all these values are zero or negative. 当所有这些值为零或负值时,您需要添加一些代码来处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM