问题描述
以dd / MM / yyyy格式在文本框中输入日期
下面给出了计算年龄的函数。
输入日期为12/02/2000时是2000年2月12日这个函数计算年龄。
但是当这一天是26/02/2000或者日> 12时,不工作。
函数calculateAge(elt){
bdate = new Date(elt.value);
birthMonth = bdate.getMonth();
birthDay = bdate.getDate();
birthYear = bdate.getFullYear();
todayDate = new Date();
todayYear = todayDate.getFullYear();
todayMonth = todayDate.getMonth();
todayDay = todayDate.getDate();
age = todayYear - birthYear;
alert(birthMonth);
if(todayMonth< birthMonth - 1){
age--;
}
if(b irthMonth - 1 == todayMonth && todayDay< birthDay){
age--;
}
返回年龄;
}
When enter date in textbox in dd/MM/yyyy format
the function for calculate age is given below.
when enter date as 12/02/2000 which is 12 feb 2000 this function calculate age.
but when the day is 26/02/2000 or day>12,not work.
function calculateAge(elt) {
bdate = new Date(elt.value);
birthMonth = bdate.getMonth();
birthDay = bdate.getDate();
birthYear = bdate.getFullYear();
todayDate = new Date();
todayYear = todayDate.getFullYear();
todayMonth = todayDate.getMonth();
todayDay = todayDate.getDate();
age = todayYear - birthYear;
alert(birthMonth);
if (todayMonth < birthMonth - 1) {
age--;
}
if (birthMonth - 1 == todayMonth && todayDay < birthDay) {
age--;
}
return age;
}
推荐答案
bdate = GetDateObject (elt.value);
var GetDateObject = function ( dateString){
var array = dateString.split('/');
var day = parseInt(array[0]);
var month = parseInt(array[1]);
var year = parseInt(array[2]);
var dateObject = new Date(year, month - 1, day);
return dateObject;
}
这篇关于日期格式为dd / MM / yyyy格式,但javascript将其视为MM / dd / yyyy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!