2016-05-23 19 views
0

年、月、日を計算するためにこのコードを取得しようとしましたが、以下のコードは常に1ヶ月を計算しています。年、月、日を計算します

$(document).on("pagecreate","#pagethree", function(){ 
    $("#btnCalc").on("click", function(e){ 
     var born = $("#born").datebox('getTheDate'); 
     var death = $("#death").datebox('getTheDate'); 
     var age = death.getFullYear() - born.getFullYear(); 
     var m = death.getMonth() - born.getMonth(); 
     var da = death.getDate() - born.getDate(); 

     if (m < 0 || (m === 0 && death.getDate() < born.getDate())) { 
      age--; 
     } 
     if(m<0){ 
      m +=12; 
     } 
     if(da<0){ 
      da +=30; 
     } 
     $("#p").popup("open"); 
     $('#altertext').html((age) + " Years "+ (Math.abs(m))+ " Months " + (Math.abs(da)) + " Days"); 
    }); 
}) 

私はこの問題を解決するにはどうすればよいですか?

+3

[日付が複雑である](http://stackoverflow.com/questions/6841333/why-is-subtracting-these-two-times-in:私はちょうどif文これにM--追加しました-1927-奇妙な結果を与える)。 [moment.js](http://momentjs.com/)のようなライブラリの使用を検討してください。 – ssube

+1

空の 'if()'は何をしていますか? – IMTheNachoMan

答えて

0

なぜ日数の計算をしないのですか?

$(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]; 
} 

正確でなければなりません。

0

ありがとうございました!

if(da<0){ 
     da +=30; 
     m--; 
    } 
関連する問題