2011-04-02 289 views
11

我錯過了下面的jQuery代碼中的東西?jquery年齡計算

var dob = $('#date').val(); 
if(dob != ''){ 
    var today = new Date(); 
    var dayDiff = Math.ceil(today - dob)/(1000 * 60 * 60 * 24 * 365); 
    var age = parseInt(dayDiff); 
    $('#age').html(age+' years old'); 
} 

我從MySQL數據庫獲取#date的預取值。

<input type="text" value="1988-04-07" id="#date" name="dob" /><p id="age"></p> 

它返回NaN,而不是正確的值。

+2

'id =「#date」'??? – esqew 2011-04-02 17:41:41

+1

不,它可能會返回'NaN',代表「不是數字」。 – 2011-04-02 17:57:46

回答

35

$('#date').val()返回字符串'1988-04-07'。你需要把它解析成一個實際的數字。

dob = new Date(dob); 
var today = new Date(); 
var age = Math.floor((today-dob)/(365.25 * 24 * 60 * 60 * 1000)); 
$('#age').html(age+' years old'); 

由於@esqew所指出的,你也需要改變id="#date"id="date"

+0

我錯過了,謝謝。 – seoppc 2011-04-02 17:57:13

+0

@ matt-ball'dob = new Date(dob); 「如果日數大於12,這種轉換對我來說就不合適了。爲什麼那個..你能幫我解決這個問題嗎? – Aju 2017-06-14 06:47:45

2
function getAge(dateString) { 
    var now = new Date(); 
    var today = new Date(now.getYear(),now.getMonth(),now.getDate()); 

    var yearNow = now.getYear(); 
    var monthNow = now.getMonth(); 
    var dateNow = now.getDate(); 
//date must be mm/dd/yyyy 
    var dob = new Date(dateString.substring(6,10), 
        dateString.substring(0,2)-1,     
        dateString.substring(3,5)     
        ); 

    var yearDob = dob.getFullYear(); 
    var monthDob = dob.getMonth(); 
    var dateDob = dob.getDate(); 
    var age = {}; 
    var ageString = ""; 
    var yearString = ""; 
    var monthString = ""; 
    var dayString = ""; 


    yearAge = yearNow - yearDob; 

    if (monthNow >= monthDob) 
    var monthAge = monthNow - monthDob; 
    else { 
    yearAge--; 
    var monthAge = 12 + monthNow -monthDob; 
    } 

    if (dateNow >= dateDob) 
    var dateAge = dateNow - dateDob; 
    else { 
    monthAge--; 
    var dateAge = 31 + dateNow - dateDob; 

    if (monthAge < 0) { 
     monthAge = 11; 
     yearAge--; 
    } 
    } 

    age = { 
     years: yearAge, 
     months: monthAge, 
     days: dateAge 
     }; 

    if (age.years > 1) yearString = " years"; 
    else yearString = " year"; 
    if (age.months> 1) monthString = " months"; 
    else monthString = " month"; 
    if (age.days > 1) dayString = " days"; 
    else dayString = " day"; 


    if ((age.years > 0) && (age.months > 0) && (age.days > 0)) 
    ageString = age.years + yearString + ", " + age.months + monthString + ", and " + age.days + dayString + " old."; 
    else if ((age.years == 0) && (age.months == 0) && (age.days > 0)) 
    ageString = "Only " + age.days + dayString + " old!"; 
    else if ((age.years > 0) && (age.months == 0) && (age.days == 0)) 
    ageString = age.years + yearString + " old. Happy Birthday!!"; 
    else if ((age.years > 0) && (age.months > 0) && (age.days == 0)) 
    ageString = age.years + yearString + " and " + age.months + monthString + " old."; 
    else if ((age.years == 0) && (age.months > 0) && (age.days > 0)) 
    ageString = age.months + monthString + " and " + age.days + dayString + " old."; 
    else if ((age.years > 0) && (age.months == 0) && (age.days > 0)) 
    ageString = age.years + yearString + " and " + age.days + dayString + " old."; 
    else if ((age.years == 0) && (age.months > 0) && (age.days == 0)) 
    ageString = age.months + monthString + " old."; 
    else ageString = "Oops! Could not calculate age!"; 

    return ageString; 
} 

// A bit of jQuery to call the getAge() function and update the page... 
$(document).ready(function() { 
    $("#submitDate").click(function(e) { 
    e.preventDefault(); 

    $("#age").html(getAge($("input#date").val())); 

    }); 
}); 

and HTML IS 
+0

var yearDob = dob.getYear(); 應該是 var yearDob = dob.getFullYear(); – Mani 2016-03-17 08:17:28

1

首先在文本中提到的ID不應包含「#」

我還創建了一個fiddle

哎呀我had't檢查它被張貼的日期

0
$("#dob").change(function(){ 

    var today = new Date(); 
    var birthDate = new Date($('#dob').val()); 
    var age = today.getFullYear() - birthDate.getFullYear(); 
    var m = today.getMonth() - birthDate.getMonth(); 
    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) { 
     age--; 
    } 
    return $('#age').html(age+' years old'); 
}); 

它對我有用