简体   繁体   中英

replace zero(integer) with space(string) with javascript

I am calculating age from DOB but while i am returning value it show '0' for more see below code

 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 
 }

How i can show space insted of '0'

replace it 'null' to null . null it is not string.

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 
}

I did as little modification to your code as I could to not modify it's structure. The main change is to check if age is zero or less than zero, because the original code did return 0 or even negative numbers if the birth date was in the last year or in the future. This should work.

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 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM