なぜ日数の計算をしないのですか?
$(document).on("pagecreate","#pagethree", function()
{
$("#btnCalc").on("click", function(e)
{
var born = $("#born").datebox('getTheDate');
var death = $("#death").datebox('getTheDate');
// you can subtract two date objects to get the milliseconds in between
// then figure out how many days that is
// 1 day = 24 hours
// 1 hour = 60 minutes
// 1 minute = 60 seconds
// 1 second = 1000 milliseconds
var ageInDays = (death - born)/1000/60/60/24;
// days/365 tells you years
var years = Math.floor(ageInDays/365);
// the remainder tells you months
var months = Math.floor(ageInDays % 365/31);
// the remainder tells you days
var days = Math.floor(ageInDays % 365 % 31);
$("#p").popup("open");
$('#altertext').html(years + " Years " + months + " Months " + days + " Days");
});
})
もちろん、月と年の日数が異なることは考慮されていません。
もっと正確なものをご希望の場合は、@ssubeのようにしてライブラリを使用してください。
** EDIT **
これは動作し、またそれは、基本的に、我々は終わりを満たすまで何年してからスタートヶ月を追加することで機能します
// calculate difference between two dates in years, months, days
function dateDiff(start, end)
{
// set a new temp variable we're gonna modify
var s = new Date(start.getTime());
// how many years
var years = end.getFullYear() - start.getFullYear();
// add this many years to the temp var
s.setFullYear(s.getFullYear() + years);
// if we've passed the end then we need to go back one year
if(s > end)
{
years--;
s.setFullYear(s.getFullYear() - 1);
}
// how many months between the two
var months = 12 - s.getMonth() + end.getMonth();
// add this many months to the temp var
s.setMonth(s.getMonth() + months);
// if we've passed the end then we need to back one month
if(s > end)
{
months--;
s.setMonth(s.getMonth() - 1);
}
// now just calculate the days between the end and temp start
var days = Math.floor((end - s)/1000/60/60/24)
return [years, months, days];
}
正確でなければなりません。
[日付が複雑である](http://stackoverflow.com/questions/6841333/why-is-subtracting-these-two-times-in:私はちょうどif文これにM--追加しました-1927-奇妙な結果を与える)。 [moment.js](http://momentjs.com/)のようなライブラリの使用を検討してください。 – ssube
空の 'if()'は何をしていますか? – IMTheNachoMan