我正在计算DOB的年龄,但是当我返回值时,它会显示' 0'更多见下面的代码
var birth = new Date('DOB');
var curr = new Date();
var age; // this is integer type in database
if(birth != null)
{
var diff = curr - birth;
age= Math.floor(diff / (1000 * 60 * 60 * 24 * 365.25));
} else {
age=' '; // while i return space from here it shows '0' on my output
}
我如何展示' 0'
的空间答案 0 :(得分:0)
将null
替换为var curr = new Date();
var age; // this is integer type in database
if(R2 != null && R2 != undefined )
{
var diff = curr - birth;
AGE= Math.floor(diff / (1000 * 60 * 60 * 24 * 365.25));
}else
AGE=' '; // while i return space from here it shows '0' on my output
}
。 null它不是字符串。
{{1}}
答案 1 :(得分:0)
我对你的代码做了很少的修改,因为我无法修改它的结构。主要变化是检查age
是零还是小于零,因为如果出生日期是去年,原始代码确实返回0
或甚至负数或将来。这应该有用。
var birth = new Date('DOB');
var curr = new Date();
var age; // this is integer type in database
if (birth != null)
{
var diff = curr - birth;
age = Math.floor(diff / (1000 * 60 * 60 * 24 * 365.25));
if (age < 1) {
age = ' ';
}
} else {
age = ' '; // while i return space from here it shows '0' on my output
}