用javascript替换零(整数)空格(字符串)

时间:2015-09-01 15:35:16

标签: javascript

我正在计算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'

的空间

2 个答案:

答案 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 
}