使用javascript的生日计算器(包括月份和日期)

时间:2019-07-01 12:00:54

标签: javascript html

我正在尝试创建一个生日计算器,其中包括几个月和几天,而不仅仅是几年。

但是我似乎无法得到它来发布答案,并且在发布时它是不正确的。

我仍在学习,因此,我非常感谢任何反馈,我希望这是我忽略的简单事情。

//birthday (entered by user)
function calculateDiff(){
  var FromYear = parseInt(date[2]);
  var FromMonth = parseInt(date[1]);
  var FromDay = parseInt(date[0]);


  //current date
  var ToYear = today.getFullYear();
  var ToMonth = today.getMonth() + 1;
  var ToDay = new Date();
  var years, months, days;

  //months
  months = ToMonth - FromMonth;
  if(ToDay < FromDay) {
	months = months -1;
  }


  //years
  years = ToYear = FromYear;
  if(ToMonth < FromMonth) {
	years = years - 1;
	months = months + 12;
  }

  //days
  days = Math.floor((ToDay.getTime() - (newDate(FromYear + years, FromMonth + months - 1, FromDay)).getTime()) / (24 * 60 * 60 * 1000));

  //answer order
  var answer = years + " years, " + months + " months, and " + days + " days.";

  //post answer
  document.getElementById("result").textContent =  answer;
  return {years: years, months: months, days: days};
}
	   <table>
        <tbody>
        <tr class="header">
          <th>&nbsp;</th>
          <th>Year</th>
          <th>Month</th>
          <th>Day</th>
        </tr>
        <tr>
       <th>Birth Date</th>
          <td><input  id="FromYear"  placeholder="YYYY" value=""></td>
          <td><input  id="FromMonth" placeholder="MM" value=""></td>
          <td><input  id="FromDay"   placeholder="DD" value=""></td>
        </tr>

        <tr>
          <th colspan="4">
            <button id="calculateDiff">Calculate</button>
          </th>
        </tr>
        <tr colspan="4">
          <th>You are <div id="result"></div></th>
      </tr>
      </tbody>
    </table>

1 个答案:

答案 0 :(得分:0)

您可以尝试以下代码

[TestFixtureSource(typeof(AnotherClass), "FixtureArgs")]
public class SomeTestClass: BaseRepositoryTests {
    private readonly Foo _foo;
    private readonly Bar _bar;

    public SomeTestClass(Foo foo, Bar bar)  {
        _foo = foo;
        _bar = bar;
    }

    [Test]
    public async Task SomeTest() {
        //...
    }
}

class AnotherClass
{
    static object [] FixtureArgs = {
        new object[] { new Foo { FooId = 0, FooName= "meh" }, new Bar { Email = "test.user@google.com", FirstName = "test", Surname = "user"}  },
        new object[] {  new Foo { FooId = 1, FooName= "meh" }, new Bar { Email = "test.user@google.com", FirstName = "test", Surname = "user"}  }
    };
}