Path: csiph.com!aioe.org!.POSTED!not-for-mail From: me Newsgroups: comp.lang.javascript Subject: Re: What is wrong with this If Else construction? Date: Mon, 26 Dec 2016 13:34:25 +0100 Organization: Aioe.org NNTP Server Lines: 77 Message-ID: References: <47744619-426f-4af1-8376-e2935a2570db@googlegroups.com> <2ee99c33-38ae-4097-b847-c3b7fc6ad63d@googlegroups.com> NNTP-Posting-Host: jKgAZnuVEbVszNzo6UUIpA.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org X-Notice: Filtered by postfilter v. 0.8.2 Xref: csiph.com comp.lang.javascript:31968 On 26-12-16 12:45, bart.smissaert@gmail.com wrote: > Verkeerde been uit bed gestapt? > > I have reworked this and this is the full code: > > function JS_GetAge(lDOB) > > { > > var jDOB = new Date((lDOB - (25569)) * 86400 * 1000); > //return jDOB; > > jDOBYear = jDOB.getFullYear(); > //return jDOBYear; > > jDOBMonth = jDOB.getMonth(); //will give January as 0. Fine, as we only compare months > //return jDOBMonth; > > var todayDate = new Date(); > > todayYear = todayDate.getFullYear(); > //return todayYear; > > todayMonth = todayDate.getMonth(); > //return todayMonth; > > age = todayYear - jDOBYear; > //return age; > > //alert(age.toString(10)); > > if (jDOBMonth > todayMonth) { > age--; > return age; > } > > if (jDOBMonth < todayMonth) { > return age; > } > > jDOBDay = jDOB.getDate(); > todayDay = todayDate.getDate(); > > if (jDOBDay < todayDay) { > age--; > } > > return age; > > } > > This runs but sometimes gives the wrong age where the age is 1 less than what it should be. > I have checked all the values (as you can see from the commented out early returns) and all > the values are fine. I thought maybe the problem is with non integer year values so I did > Math.round(todayYear) etc. but that made no difference. I also checked if return age; > indeed did a function exit and it did. So not sure what the problem is. > > RBS > Een simpele test levert in ieder geval op dat de volgende data fout gaan: 01-01-1970 19-12-1970 06-12-1971 07-01-1972 24-12-1972 11-12-1973 17-12-1975 03-12-1976 04-01-1977 22-12-1977 09-12-1978 14-12-1980 01-12-1981