如何获得某个生日的剩余日期?

时间:2017-06-04 04:35:29

标签: javascript calculator

当我计算我的年龄时,下面的代码显示" xxx天留给你的下一个生日"。但是我想知道,某个生日还有多少天。 (即:我想知道我的30岁生日还有多少天。结果应该是这样的" xxx天30岁生日"而不是下一个生日。) 我需要做些什么改变?



function wr_document() {
  var w = new Date();
  var s_d = w.getDate();
  var s_m = w.getMonth() + 1;
  var s_y = w.getFullYear();


  document.cir.len11.value = s_d;
  document.cir.len12.value = s_m;
  document.cir.len13.value = s_y;
}

function isNum(arg) {
  var args = arg;
  if (args == "" || args == null || args.length == 0) {
    return false;
  }
  args = args.toString();
  for (var i = 0; i < args.length; i++) {
    if ((args.substring(i, i + 1) < "0" || args.substring(i, i + 1) > "9") && args.substring(i, i + 1) != ".") {
      return false;
    }
  }
  return true;
}

function checkday(aa) {
  var val = aa.value;
  var valc = val.substring(0, 1);
  if (val.length > 0 && val.length < 3) {
    if (!isNum(val) || val == 0) {
      aa.value = "";
    } else if (val < 1 || val > 31) {
      aa.value = valc;
    }
  } else if (val.length > 2) {
    val = val.substring(0, 2);
    aa.value = val;
  }
}

function checkmon(aa) {
  var val = aa.value;
  var valc = val.substring(0, 1);
  if (val.length > 0 && val.length < 3) {
    if (!isNum(val) || val == 0) {
      aa.value = "";
    } else if (val < 1 || val > 12) {
      aa.value = valc;
    }
  } else if (val.length > 2) {
    val = val.substring(0, 2);
    aa.value = val;
  }
}

function checkyear(aa) {
  var val = aa.value;
  var valc = val.substring(0, (val.length - 1));
  if (val.length > 0 && val.length < 7) {
    if (!isNum(val) || val == 0) {
      aa.value = valc;
    } else if (val < 1 || val > 275759) {
      aa.value = "";
    }
  } else if (val.length > 4) {
    aa.value = valc;
  }
}

function checkleapyear(datea) {
  if (datea.getYear() % 4 == 0) {
    if (datea.getYear() % 10 != 0) {
      return true;
    } else {
      if (datea.getYear() % 400 == 0)
        return true;
      else
        return false;
    }
  }
  return false;
}

function DaysInMonth(Y, M) {
  with(new Date(Y, M, 1, 12)) {
    setDate(0);
    return getDate();
  }
}

function datediff(date1, date2) {
  var y1 = date1.getFullYear(),
    m1 = date1.getMonth(),
    d1 = date1.getDate(),
    y2 = date2.getFullYear(),
    m2 = date2.getMonth(),
    d2 = date2.getDate();
  if (d1 < d2) {
    m1--;
    d1 += DaysInMonth(y2, m2);
  }
  if (m1 < m2) {
    y1--;
    m1 += 12;
  }
  return [y1 - y2, m1 - m2, d1 - d2];
}

function calage() {
  var curday = document.cir.len11.value;
  var curmon = document.cir.len12.value;
  var curyear = document.cir.len13.value;
  var calday = document.cir.len21.value;
  var calmon = document.cir.len22.value;
  var calyear = document.cir.len23.value;
  if (curday == "" || curmon == "" || curyear == "" || calday == "" || calmon == "" || calyear == "") {
    alert("Please fill all the values and click 'Go'");
  } else if (curday == calday && curmon == calmon && curyear == calyear) {
    alert("Today your birthday & Your age is 0 years old")
  } else {
    var curd = new Date(curyear, curmon - 1, curday);
    var cald = new Date(calyear, calmon - 1, calday);
    var diff = Date.UTC(curyear, curmon, curday, 0, 0, 0) -
      Date.UTC(calyear, calmon, calday, 0, 0, 0);
    var dife = datediff(curd, cald);
    document.cir.val.value = dife[0] + " years, " + dife[1] + " months, and " + dife[2] + " days";
    var secleft = diff / 1000 / 60;
    document.cir.val3.value = secleft + " minutes since your birth";
    var hrsleft = secleft / 60;
    document.cir.val2.value = hrsleft + " hours since your birth";
    var daysleft = hrsleft / 24;
    document.cir.val1.value = daysleft + " days since your birth";
    //alert(""+parseInt(calyear)+"--"+dife[0]+"--"+1);
    var as = parseInt(calyear) + dife[0] + 1;
    var diff = Date.UTC(as, calmon, calday, 0, 0, 0) -
      Date.UTC(curyear, curmon, curday, 0, 0, 0);
    var datee = diff / 1000 / 60 / 60 / 24;
    document.cir.val4.value = datee + " days left for your next birthday";
  }
}

function color(test) {
  for (var j = 7; j < 12; j++) {
    var myI = document.getElementsByTagName("input").item(j);
    //myI.setAttribute("style",ch);
    myI.style.backgroundColor = test;
  }
}

function color1(test) {
  var myI = document.getElementsByTagName("table").item(0);
  //myI.setAttribute("style",ch);
  myI.style.backgroundColor = test;
}
&#13;
.cal-container {
  width: 540px;
  margin: 10px auto 0;
}

#age-calculator {
  background: none repeat scroll 0 0 #DDDDDD;
  border: 1px solid #BEBEBE;
  padding-left: 20px;
}

.calc {
  border-color: #AAAAAA #999999 #929292 #AAAAAA;
  border-style: solid;
  border-width: 1px 2px 2px 1px;
  padding: 2px 30px 3px;
  height: 27px;
}

.calc:active {
  border-color: #AAAAAA #999999 #929292 #AAAAAA;
  border-style: solid;
  border-width: 1px;
}
&#13;
<title>Age calculator </title>

<body onload="wr_document()">
  <div class="cal-container">
    <div id="calculator-container">
      <table border="0" cellpadding="0" cellspacing="0" style="width: 100%px;">
        <tbody>
          <tr>
            <td valign="top">
              <h1 style="padding-top: 10px;">
                Age Calculator</h1>
              <div class="descalign">
                <span>Calculate your age in days, years, minutes, seconds. Know how many days are left for your next birthday.</span><br /><br />
              </div>
              <div id="age-calculator">
                <table bgcolor="" border="0" cellpadding="0" cellspacing="4" style="width: 100%px;">
                  <tbody>
                    <tr>
                      <td colspan="2">
                        <table class="result" style="height: 100%px; width: 100%px;">
                          <tbody>
                            <tr>
                              <td>
                                <form name="cir">
                                  <table cellpadding="3" cellspacing="0">
                                    <tbody>
                                      <tr>
                                        <td colspan="2">
                                          <br /> Today's Date is:
                                        </td>
                                      </tr>
                                      <tr>
                                        <td align="center" colspan="2">
                                          Date -
                                          <input class="innerc resform" name="len11" onkeyup="checkday(this)" size="2" type="text" value="" /> Month -
                                          <input class="innerc resform" name="len12" onkeyup="checkmon(this)" size="2" type="text" value="" /> Year -
                                          <input class="innerc resform" name="len13" onkeyup="checkyear(this)" size="4" type="text" value="" />
                                          <br />
                                          <br />
                                        </td>
                                      </tr>
                                      <tr>
                                        <td colspan="2"> Enter Your Date of Birth : </td>
                                      </tr>
                                      <tr>
                                        <td align="center" colspan="2">
                                          Date -
                                          <input class="innerc resform" name="len21" onkeyup="checkday(this)" size="2" type="text" /> Month -
                                          <input class="innerc resform" name="len22" onkeyup="checkmon(this)" size="2" type="text" /> Year -
                                          <input class="innerc resform" name="len23" onkeyup="checkyear(this)" size="4" type="text" />
                                          <br />
                                          <br />
                                          <input class="calc" name="but" onclick="calage()" type="button" value=" Go " />
                                          <br />
                                          <br />
                                        </td>
                                      </tr>
                                      <tr>
                                        <td align="center" class="form" width="30%">
                                          <b> </b>
                                        </td>
                                      </tr>
                                    </tbody>
                                  </table>
                                  <table>
                                    <tbody>
                                      <tr>
                                        <td>
                                          <b> Your Age is </b>
                                        </td>
                                        <td>
                                          <input class="resform" name="val" readonly="" size="36" type="text" />
                                        </td>
                                      </tr>
                                      <tr>
                                        <td>
                                          <b> Your Age in Days </b>
                                        </td>
                                        <td>
                                          <input class="resform" name="val1" readonly="" size="36" type="text" />
                                        </td>
                                      </tr>
                                      <tr>
                                        <td>
                                          <b> Your Age in Hours </b>
                                        </td>
                                        <td>
                                          <input class="resform" name="val2" readonly="" size="36" type="text" /> (Approximate)
                                        </td>
                                      </tr>
                                      <tr>
                                        <td class="form">
                                          <b> Your Age in Minutes </b>
                                        </td>
                                        <td>
                                          <input class="resform" name="val3" readonly="" size="36" type="text" /> (Approximate)
                                        </td>
                                      </tr>
                                      <tr>
                                        <td> </td>
                                      </tr>
                                      <tr>
                                        <td></td>
                                        <td>
                                          <input class="innerc resform" name="val4" readonly="" size="36" type="text" />
                                        </td>
                                      </tr>
                                    </tbody>
                                  </table>
                                </form>
                              </td>
                            </tr>
                          </tbody>
                        </table>
                        <br />
                      </td>
                      <td> </td>
                    </tr>
                    <tr>
                      <td align="right" colspan="2"> </td>
                      <td> </td>
                    </tr>
                  </tbody>
                </table>
                <br />
              </div>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</body>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

不清楚整个代码的用途,但根据您的问题,以下脚本可能对您有帮助。

&#13;
&#13;
// Let below variables store your birth date
var day = 13;
var month = 03;
var year = 1993;
var x = 30; // Finding 30th birthday
var xthBirthday = new Date((year + x), (month - 1), day); // As month starts with 0
var timeForXthBirthday = xthBirthday.getTime() - Date.now();
var noOfDaysForXthBirthday = Math.ceil(timeForXthBirthday / (1000 * 60 * 60 * 24)); // No. of days left for your xth birthday
document.write(noOfDaysForXthBirthday+" days left for your "+x+"<sup>th</sup> birthday.");
&#13;
&#13;
&#13;

希望这有帮助。