Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #31962 > unrolled thread
| Started by | bart.smissaert@gmail.com |
|---|---|
| First post | 2016-12-26 00:05 -0800 |
| Last post | 2016-12-28 22:17 +0000 |
| Articles | 20 on this page of 32 — 10 participants |
Back to article view | Back to comp.lang.javascript
What is wrong with this If Else construction? bart.smissaert@gmail.com - 2016-12-26 00:05 -0800
Re: What is wrong with this If Else construction? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-12-26 10:37 +0100
Re: What is wrong with this If Else construction? bart.smissaert@gmail.com - 2016-12-26 03:45 -0800
Re: What is wrong with this If Else construction? Luuk <luuk@invalid.lan> - 2016-12-26 12:59 +0100
Re: What is wrong with this If Else construction? bart.smissaert@gmail.com - 2016-12-26 04:02 -0800
Re: What is wrong with this If Else construction? bart.smissaert@gmail.com - 2016-12-26 04:16 -0800
Re: What is wrong with this If Else construction? me <mu@local.local> - 2016-12-26 13:34 +0100
Re: What is wrong with this If Else construction? Bart Smissaert <bart.smissaert@gmail.com> - 2016-12-26 05:02 -0800
Re: What is wrong with this If Else construction? Luuk <luuk@invalid.lan> - 2016-12-26 18:32 +0100
Re: What is wrong with this If Else construction? Bart Smissaert <bart.smissaert@gmail.com> - 2016-12-26 15:32 -0800
Re: What is wrong with this If Else construction? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-12-27 10:05 +0100
Re: What is wrong with this If Else construction? Bart Smissaert <bart.smissaert@gmail.com> - 2016-12-27 03:27 -0800
Re: What is wrong with this If Else construction? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-12-27 21:46 +0100
Re: What is wrong with this If Else construction? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-12-29 18:36 +0100
Re: What is wrong with this If Else construction? Dr J R Stockton <reply1600@merlyn.demon.co.uk.invalid> - 2016-12-28 23:16 +0000
Re: What is wrong with this If Else construction? Luuk <luuk@invalid.lan> - 2016-12-27 22:28 +0100
Re: What is wrong with this If Else construction? Gene Wirchenko <genew@telus.net> - 2016-12-27 15:07 -0800
Re: What is wrong with this If Else construction? Luuk <luuk@invalid.lan> - 2016-12-28 19:37 +0100
Re: What is wrong with this If Else construction? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-12-26 21:56 +0100
Re: What is wrong with this If Else construction? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-12-26 22:14 +0100
Re: What is wrong with this If Else construction? Gene Wirchenko <genew@telus.net> - 2016-12-26 14:12 -0800
Re: What is wrong with this If Else construction? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-12-27 00:02 +0100
Re: What is wrong with this If Else construction? Dr J R Stockton <reply1600@merlyn.demon.co.uk.invalid> - 2016-12-27 22:36 +0000
Re: What is wrong with this If Else construction? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-12-28 10:36 +0100
Re: What is wrong with this If Else construction? Luuk <luuk@invalid.lan> - 2016-12-26 17:00 +0100
OT: Leap year bug in Excel (was: What is wrong with this If Else construction?) "Christoph M. Becker" <cmbecker69@arcor.de> - 2016-12-26 17:12 +0100
Re: What is wrong with this If Else construction? Dr J R Stockton <reply1600@merlyn.demon.co.uk.invalid> - 2016-12-27 22:52 +0000
Re: What is wrong with this If Else construction? "Christoph M. Becker" <cmbecker69@arcor.de> - 2016-12-28 01:01 +0100
Re: What is wrong with this If Else construction? $Bill <news@todbe.com> - 2016-12-27 22:19 -0800
Re: What is wrong with this If Else construction? Tim Streater <timstreater@greenbee.net> - 2016-12-28 10:05 +0000
Re: What is wrong with this If Else construction? [OT] $Bill <news@todbe.com> - 2016-12-28 13:59 -0800
Re: What is wrong with this If Else construction? [OT] Tim Streater <timstreater@greenbee.net> - 2016-12-28 22:17 +0000
Page 1 of 2 [1] 2 Next page →
| From | bart.smissaert@gmail.com |
|---|---|
| Date | 2016-12-26 00:05 -0800 |
| Subject | What is wrong with this If Else construction? |
| Message-ID | <47744619-426f-4af1-8376-e2935a2570db@googlegroups.com> |
New to JavaScript and trying to calculate the date of birth from an Excel date, provided as an integer value (1 Jan 1900 = 1).
Having trouble to get the right syntax for this block of code:
If (today_month < M) {
age--;
}
else {
F = parseInt(30.6001 * E);
DOM = (B - (D + F)) + (Q - Z); //day of the month
If (DOM > today_day) {
age--;
}
}
Tried various constructions but no success sofar. Note that the 2 lines
F = .... and DOM = ... only need to run if (today_month < M) is false.
I tried with those 2 lines outside the If Else block, but no success either.
Thanks for any advice.
RBS
[toc] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2016-12-26 10:37 +0100 |
| Message-ID | <XnsA6EA6C0A11982eejj99@194.109.6.166> |
| In reply to | #31962 |
bart.smissaert@gmail.com wrote on 26 Dec 2016 in comp.lang.javascript:
> New to JavaScript and trying to calculate the date of birth from an
> Excel date, provided as an integer value (1 Jan 1900 = 1). Having
> trouble to get the right syntax for this block of code:
There is no "THE right syntax",
there is code that does not do what you want it to do,
and that does.
> If (today_month < M) {
> age--;
> }
> else {
> F = parseInt(30.6001 * E);
> DOM = (B - (D + F)) + (Q - Z); //day of the month
You are talking in riddles, not declaring and not defining variables.
> If (DOM > today_day) {
> age--;
> }
> }
>
> Tried various constructions but no success sofar.
You should first define what you want.
Then define how you want to do it, perhaps in pseudocode.
Then implement it in a way you understand.
> Note that the 2 lines
> F = .... and DOM = ... only need to run if (today_month < M) is false.
Lines don't run, they are executed, or not.
Try symplified:
1 <script type='text/javascript'>
2 If (true)
3 { alert('first line executes') }
4 else
5 { alert('second line executes') };
6 </script>
> I tried with those 2 lines outside the If Else block, but no success
> either.
Did you try debugging?
If so, you should have been aware that 'If' is not 'if'.
The Google Chrome console responds with:
"Uncaught SyntaxError: Unexpected token else test.html:4"
Explanation: the 'else' on line 4 is unexpected because there is no 'if'!!!!
'If' should be 'if' as javascript is case-sensitive,
so you should think[!] case-sensitive too.
> Thanks for any advice.
Using code you found on the web,
that you don't understand,
is bound to fail you,
and is bound to keep you in your self-professed newbee-status.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | bart.smissaert@gmail.com |
|---|---|
| Date | 2016-12-26 03:45 -0800 |
| Message-ID | <2ee99c33-38ae-4097-b847-c3b7fc6ad63d@googlegroups.com> |
| In reply to | #31963 |
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
On Monday, 26 December 2016 09:37:22 UTC, Evertjan. wrote:
> bart.smissaert@gmail.com wrote on 26 Dec 2016 in comp.lang.javascript:
>
> > New to JavaScript and trying to calculate the date of birth from an
> > Excel date, provided as an integer value (1 Jan 1900 = 1). Having
> > trouble to get the right syntax for this block of code:
>
> There is no "THE right syntax",
> there is code that does not do what you want it to do,
> and that does.
>
> > If (today_month < M) {
> > age--;
> > }
> > else {
> > F = parseInt(30.6001 * E);
> > DOM = (B - (D + F)) + (Q - Z); //day of the month
>
> You are talking in riddles, not declaring and not defining variables.
>
> > If (DOM > today_day) {
> > age--;
> > }
> > }
> >
> > Tried various constructions but no success sofar.
>
> You should first define what you want.
> Then define how you want to do it, perhaps in pseudocode.
> Then implement it in a way you understand.
>
> > Note that the 2 lines
> > F = .... and DOM = ... only need to run if (today_month < M) is false.
>
> Lines don't run, they are executed, or not.
>
> Try symplified:
>
> 1 <script type='text/javascript'>
> 2 If (true)
> 3 { alert('first line executes') }
> 4 else
> 5 { alert('second line executes') };
> 6 </script>
>
>
> > I tried with those 2 lines outside the If Else block, but no success
> > either.
>
> Did you try debugging?
>
> If so, you should have been aware that 'If' is not 'if'.
>
> The Google Chrome console responds with:
>
> "Uncaught SyntaxError: Unexpected token else test.html:4"
>
> Explanation: the 'else' on line 4 is unexpected because there is no 'if'!!!!
>
> 'If' should be 'if' as javascript is case-sensitive,
> so you should think[!] case-sensitive too.
>
> > Thanks for any advice.
>
> Using code you found on the web,
> that you don't understand,
> is bound to fail you,
> and is bound to keep you in your self-professed newbee-status.
>
>
> --
> Evertjan.
> The Netherlands.
> (Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Luuk <luuk@invalid.lan> |
|---|---|
| Date | 2016-12-26 12:59 +0100 |
| Message-ID | <5861061e$0$21528$e4fe514c@news.xs4all.nl> |
| In reply to | #31964 |
On 26-12-16 12:45, bart.smissaert@gmail.com wrote: > This runs but sometimes gives the wrong age where the age is 1 less than what it should be When is 'sometimes' ? Only on monday mornings? Or also on Friday afternoons ...
[toc] | [prev] | [next] | [standalone]
| From | bart.smissaert@gmail.com |
|---|---|
| Date | 2016-12-26 04:02 -0800 |
| Message-ID | <2e9028f2-b52c-40cd-9aff-5b18c32cdc68@googlegroups.com> |
| In reply to | #31965 |
On Monday, 26 December 2016 11:59:31 UTC, Luuk wrote: > On 26-12-16 12:45, bart.smissaert@gmail.com wrote: > > This runs but sometimes gives the wrong age where the age is 1 less than what it should be > > When is 'sometimes' ? > > Only on monday mornings? > > Or also on Friday afternoons ... I said sometimes as I can see no obvious pattern. If I could I certainly would have mentioned it. RBS
[toc] | [prev] | [next] | [standalone]
| From | bart.smissaert@gmail.com |
|---|---|
| Date | 2016-12-26 04:16 -0800 |
| Message-ID | <f60d65e2-0d9a-4e46-8a20-dd42f4b8bdb5@googlegroups.com> |
| In reply to | #31966 |
On Monday, 26 December 2016 12:02:41 UTC, bart.sm...@gmail.com wrote: > On Monday, 26 December 2016 11:59:31 UTC, Luuk wrote: > > On 26-12-16 12:45, bart.smissaert@gmail.com wrote: > > > This runs but sometimes gives the wrong age where the age is 1 less than what it should be > > > > When is 'sometimes' ? > > > > Only on monday mornings? > > > > Or also on Friday afternoons ... > > I said sometimes as I can see no obvious pattern. > If I could I certainly would have mentioned it. > > RBS Forget about this, I think I found the problem. RBS
[toc] | [prev] | [next] | [standalone]
| From | me <mu@local.local> |
|---|---|
| Date | 2016-12-26 13:34 +0100 |
| Message-ID | <o3r2of$7us$2@gioia.aioe.org> |
| In reply to | #31964 |
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
[toc] | [prev] | [next] | [standalone]
| From | Bart Smissaert <bart.smissaert@gmail.com> |
|---|---|
| Date | 2016-12-26 05:02 -0800 |
| Message-ID | <e4aac875-685d-4fe8-a06c-132f8f961b68@googlegroups.com> |
| In reply to | #31968 |
On Monday, 26 December 2016 12:34:28 UTC, me wrote:
> 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
Not sure why this is marked as posted by me.
I fixed matters (code below) and have tested on some 12000 random dates
and all show correct age.
I tested your 01-12-1981 and that produced correct age as well.
Maybe you should show your code.
Current code that seems to work fine:
function JS_GetAge5(lDOB)
//Change constant functions to literals
//-----------------------------------------
{
var jDOB = new Date((lDOB - (25569)) * 86400 * 1000);
//return jDOB;
jDOBYear = jDOB.getFullYear();
//return jDOBYear;
jDOBMonth = jDOB.getMonth() + 1; //as getMonth is 0-based!
//return jDOBMonth;
var todayDate = new Date();
todayYear = todayDate.getFullYear();
//return todayYear;
todayMonth = todayDate.getMonth(); //no + 1 here as this will be replaced by VB6 to a literal!
//return todayMonth;
age = todayYear - jDOBYear;
//return Math.round(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;
}
RBS
[toc] | [prev] | [next] | [standalone]
| From | Luuk <luuk@invalid.lan> |
|---|---|
| Date | 2016-12-26 18:32 +0100 |
| Message-ID | <58615438$0$21427$e4fe514c@news.xs4all.nl> |
| In reply to | #31970 |
On 26-12-16 14:02, Bart Smissaert wrote:
> On Monday, 26 December 2016 12:34:28 UTC, me wrote:
>> On 26-12-16 12:45, bart.smissaert@gmail.com wrote:
>>
>>
>> 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
>
> Not sure why this is marked as posted by me.
> I fixed matters (code below) and have tested on some 12000 random dates
> and all show correct age.
> I tested your 01-12-1981 and that produced correct age as well.
> Maybe you should show your code.
>
> Current code that seems to work fine:
>
> function JS_GetAge5(lDOB)
>
> //Change constant functions to literals
> //-----------------------------------------
>
> {
>
> var jDOB = new Date((lDOB - (25569)) * 86400 * 1000);
> //return jDOB;
>
> jDOBYear = jDOB.getFullYear();
> //return jDOBYear;
>
> jDOBMonth = jDOB.getMonth() + 1; //as getMonth is 0-based!
> //return jDOBMonth;
>
> var todayDate = new Date();
>
> todayYear = todayDate.getFullYear();
> //return todayYear;
>
> todayMonth = todayDate.getMonth(); //no + 1 here as this will be replaced by VB6 to a literal!
> //return todayMonth;
>
> age = todayYear - jDOBYear;
> //return Math.round(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;
>
> }
>
>
> RBS
>
OK, my first attempt might have been wrong.
But this one ...
I created a CSV file with some help from Excel:
35761;27-11-1997;19;
35943;28-05-1998;18;
36125;26-11-1998;18;
36307;27-05-1999;17;
36489;25-11-1999;17;
36671;25-05-2000;16;
36853;23-11-2000;16;
37035;24-05-2001;15;
37217;22-11-2001;15;
37399;23-05-2002;14;
....
45225;26-10-2023;-7;
29921;01-12-1981;35;
It had 3 columns,
first: date as number (since 1-1-1900)
second: date (DD-MM-JJJJ)
age; calculated in Excel.
I added this yo your code:
== begin of code ==
var lineReader = require('readline').createInterface({
input: require('fs').createReadStream('LEEFTIJD.csv')
});
lineReader.on('line', function (line) {
//console.log('CSV file:', line);
var arr = line.split(";");
var datumXLS = arr[1];
var leeftijdCSV = Number(arr[2]);
var leeftijd = JS_GetAge5(arr[0]);
if (!( leeftijd == leeftijdCSV )) console.log ( leeftijd,
leeftijdCSV, datumXLS);
});
== end of code ==
This reads the csv-file line by line, and compare the age with the
output of your function.
Output:
41 42 '26-12-1974'
40 41 '25-12-1975'
39 40 '23-12-1976'
38 39 '22-12-1977'
37 38 '21-12-1978'
36 37 '20-12-1979'
35 36 '18-12-1980'
34 35 '17-12-1981'
33 34 '16-12-1982'
32 33 '15-12-1983'
31 32 '13-12-1984'
30 31 '12-12-1985'
29 30 '11-12-1986'
28 29 '10-12-1987'
27 28 '08-12-1988'
26 27 '07-12-1989'
25 26 '06-12-1990'
24 25 '05-12-1991'
23 24 '03-12-1992'
22 23 '02-12-1993'
21 22 '01-12-1994'
20 21 '30-11-1995'
19 20 '28-11-1996'
18 19 '27-11-1997'
34 35 '01-12-1981'
[toc] | [prev] | [next] | [standalone]
| From | Bart Smissaert <bart.smissaert@gmail.com> |
|---|---|
| Date | 2016-12-26 15:32 -0800 |
| Message-ID | <f7217774-4e2d-49e6-b03b-a21aec7980b1@googlegroups.com> |
| In reply to | #31979 |
Look at the JS code I posted, especially the comments and you may figure out what is going on. RBS
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2016-12-27 10:05 +0100 |
| Message-ID | <XnsA6EB66B6EE392eejj99@194.109.6.166> |
| In reply to | #31995 |
Bart Smissaert <bart.smissaert@gmail.com> wrote on 27 Dec 2016 in comp.lang.javascript: > Look at the JS code I posted, Where? What? > especially the comments and you may figure > out what is going on. Are you responding to someone? Please always quote relevant parts. Please quote, this is not email, not GG, this is Usenet. -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Bart Smissaert <bart.smissaert@gmail.com> |
|---|---|
| Date | 2016-12-27 03:27 -0800 |
| Message-ID | <505046fc-12c8-4dcc-8ae6-c3caf7470b1a@googlegroups.com> |
| In reply to | #31997 |
On Tuesday, 27 December 2016 09:05:53 UTC, Evertjan. wrote:
> Bart Smissaert <bart.smissaert@gmail.com> wrote on 27 Dec 2016 in
> comp.lang.javascript:
>
> > Look at the JS code I posted,
>
> Where? What?
>
> > especially the comments and you may figure
> > out what is going on.
>
> Are you responding to someone?
>
> Please always quote relevant parts.
>
> Please quote,
> this is not email, not GG,
> this is Usenet.
>
> --
> Evertjan.
> The Netherlands.
> (Please change the x'es to dots in my emailaddress)
Look at the beginning of the last mail I posted with JS code:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
function JS_GetAge5(lDOB)
//Change constant functions to literals
//-----------------------------------------
{
var jDOB = new Date((lDOB - (25569)) * 86400 * 1000);
//return jDOB;
jDOBYear = jDOB.getFullYear();
//return jDOBYear;
jDOBMonth = jDOB.getMonth() + 1; //as getMonth is 0-based!
//return jDOBMonth;
var todayDate = new Date();
todayYear = todayDate.getFullYear();
//return todayYear;
todayMonth = todayDate.getMonth(); //no + 1 here as this will be replaced by VB6 to a literal!
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Maybe now you understand.
If not then change this line:
jDOBMonth = jDOB.getMonth() + 1;
to this:
jDOBMonth = jDOB.getMonth();
and test again.
RBS
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2016-12-27 21:46 +0100 |
| Message-ID | <XnsA6EBDD7D05C4Aeejj99@194.109.6.166> |
| In reply to | #31998 |
Bart Smissaert <bart.smissaert@gmail.com> wrote on 27 Dec 2016 in
comp.lang.javascript:
> On Tuesday, 27 December 2016 09:05:53 UTC, Evertjan. wrote:
>> Bart Smissaert <bart.smissaert@gmail.com> wrote on 27 Dec 2016 in
>> comp.lang.javascript:
>>
>> > Look at the JS code I posted,
>>
>> Where? What?
>>
>> > especially the comments and you may figure
>> > out what is going on.
>>
>> Are you responding to someone?
>>
>
>> Please always quote relevant parts.
>>
>> Please quote,
>> this is not email, not GG,
>> this is Usenet.
[Please do not quote signatures on usenet. Removed]
> Look at the beginning of the last mail I posted with JS code:
>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
> function JS_GetAge5(lDOB)
>
> //Change constant functions to literals
> //-----------------------------------------
>
>
> {
>
> var jDOB = new Date((lDOB - (25569)) * 86400 * 1000);
> //return jDOB;
>
> jDOBYear = jDOB.getFullYear();
> //return jDOBYear;
>
>
> jDOBMonth = jDOB.getMonth() + 1; //as getMonth is 0-based!
>
> //return jDOBMonth;
>
> var todayDate = new Date();
>
> todayYear = todayDate.getFullYear();
> //return todayYear;
>
>
> todayMonth = todayDate.getMonth(); //no + 1 here as this will be
> replaced by VB6 to a literal!
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>
> Maybe now you understand.
This was not a Q about understanding,
but about you not quoting and adressing an unknown "you".
> If not then change this line:
>
> jDOBMonth = jDOB.getMonth() + 1;
>
> to this:
>
> jDOBMonth = jDOB.getMonth();
>
> and test again.
No,
I will not test anything,
you should test and demonstrate what debugging your errors shows.
=============
Methinks the most efficient way of determining an age
against the present day is
[I am not going into Excel quircks here]
in pseudocode[!!]:
age = presentYear - birthYear;
if (setFullYear(birthYear,birthMonth-1,birthDate) <
setFullYear(birthYear,presentMonth-1,presentDate))
age--;
[setFullYear will correct for non-leapyears]
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2016-12-29 18:36 +0100 |
| Message-ID | <XnsA6EDBD4CF42A5eejj99@194.109.6.166> |
| In reply to | #31999 |
Dr J R Stockton <reply1600@merlyn.demon.co.uk.invalid> wrote on 29 Dec 2016 in comp.lang.javascript: > In comp.lang.javascript message <XnsA6EBDD7D05C4Aeejj99@194.109.6.166>, > Tue, 27 Dec 2016 21:46:23, Evertjan. <exxjxw.hannivoort@inter.nl.net> > posted: > >> >>age = presentYear - birthYear; >>if (setFullYear(birthYear,birthMonth-1,birthDate) < >> setFullYear(birthYear,presentMonth-1,presentDate)) >> age--; >> > > I rather suspect that the boolean expression will fail if the two dates > are the same but in one case the beginning of the date is in Winter Time > and the other case is in Summer Time or /vice versa/. To see this, it > will probably be necessary to be using UK time or Azores Time. I don't thinks so, as this is about dates without time, so whether there is a 1 hour difference would make a difference with a boolean equal [==], NOT with a boolen less than [<], as the difference being 23, 24 or 25 hours surly won't matter. > There is, of course, the question of someone born in a distant Time > Zone. No, because common age in years in this sense does not imply that your become some age at 15 hours of your birthday, you become that age at zero hours local time wherever you are in the world. If not you would probably also have to account for a 0.24.. leapday correction becoming setting that hour nearly 3 hours earlier for 3 years and then nearly 9 hours later in the 4th year, the 'nearly' accounting for the 100 year mean. Sillyness of sillynesses? > Use setUTCFullYear instead, to remove Summer Time concern; and, AISB, > much simpler to use simple arithmetic to compare, for example by using > the ISO8601 YYYYMMDD date strings. > > For greater ages, such as those of buildings, there is another problem, > as is illustrated by the following :- AIUI, Miguel Cervantes was buried > on 1616 04 23 (the day after his death) - and William Shakespeare died > on 1616 04 23, ten days later. So then you simply would have to include what time definitions you use, if they are different, leading to the same sillyness of sillynesses, as we are not striving for such extreme exactness. Only in law [age of majority, age of sexual consent, marriageable age, school leaving age, drinking age, driving age, voting age, smoking age] the exact date would have consquences for Miguel and William. -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Dr J R Stockton <reply1600@merlyn.demon.co.uk.invalid> |
|---|---|
| Date | 2016-12-28 23:16 +0000 |
| Message-ID | <ywbVJtWmfEZYFw3K@invalid.uk.co.demon.merlyn.invalid> |
| In reply to | #31999 |
In comp.lang.javascript message <XnsA6EBDD7D05C4Aeejj99@194.109.6.166>, Tue, 27 Dec 2016 21:46:23, Evertjan. <exxjxw.hannivoort@inter.nl.net> posted: > >age = presentYear - birthYear; >if (setFullYear(birthYear,birthMonth-1,birthDate) < > setFullYear(birthYear,presentMonth-1,presentDate)) > age--; > I rather suspect that the boolean expression will fail if the two dates are the same but in one case the beginning of the date is in Winter Time and the other case is in Summer Time or /vice versa/. To see this, it will probably be necessary to be using UK time or Azores Time. There is, of course, the question of someone born in a distant Time Zone. Use setUTCFullYear instead, to remove Summer Time concern; and, AISB, much simpler to use simple arithmetic to compare, for example by using the ISO8601 YYYYMMDD date strings. For greater ages, such as those of buildings, there is another problem, as is illustrated by the following :- AIUI, Miguel Cervantes was buried on 1616 04 23 (the day after his death) - and William Shakespeare died on 1616 04 23, ten days later. -- (c) John Stockton, Surrey, UK. ¬@merlyn.demon.co.uk Turnpike v6.05 MIME. Merlyn Web Site < > - FAQish topics, acronyms, & links.
[toc] | [prev] | [next] | [standalone]
| From | Luuk <luuk@invalid.lan> |
|---|---|
| Date | 2016-12-27 22:28 +0100 |
| Message-ID | <5862dd01$0$21499$e4fe514c@news.xs4all.nl> |
| In reply to | #31995 |
On 27-12-16 00:32, Bart Smissaert wrote: > Look at the JS code I posted, especially the comments and you may figure out what is going on. > > RBS > I do not have a need to calculate my age ... It's too embarassing (for /me)
[toc] | [prev] | [next] | [standalone]
| From | Gene Wirchenko <genew@telus.net> |
|---|---|
| Date | 2016-12-27 15:07 -0800 |
| Message-ID | <avs56ctobsv5frgm3i166g15jt3fn6738u@4ax.com> |
| In reply to | #32000 |
On Tue, 27 Dec 2016 22:28:36 +0100, Luuk <luuk@invalid.lan> wrote:
>On 27-12-16 00:32, Bart Smissaert wrote:
>> Look at the JS code I posted, especially the comments and you may figure out what is going on.
>>
>> RBS
>>
>
>I do not have a need to calculate my age ...
>
>It's too embarassing (for /me)
You have survived this long. What is the embarrassment?
Sincerely,
Gene Wirchenko
[toc] | [prev] | [next] | [standalone]
| From | Luuk <luuk@invalid.lan> |
|---|---|
| Date | 2016-12-28 19:37 +0100 |
| Message-ID | <5864067f$0$21428$e4fe514c@news.xs4all.nl> |
| In reply to | #32001 |
On 28-12-16 00:07, Gene Wirchenko wrote: > On Tue, 27 Dec 2016 22:28:36 +0100, Luuk <luuk@invalid.lan> wrote: > >> On 27-12-16 00:32, Bart Smissaert wrote: >>> Look at the JS code I posted, especially the comments and you may figure out what is going on. >>> >>> RBS >>> >> >> I do not have a need to calculate my age ... >> >> It's too embarassing (for /me) > > You have survived this long. What is the embarrassment? > > Sincerely, > > Gene Wirchenko > ;)
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2016-12-26 21:56 +0100 |
| Message-ID | <XnsA6EADF22299AAeejj99@194.109.6.166> |
| In reply to | #31964 |
bart.smissaert@gmail.com wrote on 26 Dec 2016 in comp.lang.javascript:
> Verkeerde been uit bed gestapt?
Please do not toppost on usenet.
I can understand you get frustrated,
when I show you this first and catastrophic mistake
in your Javascript-code [Using "If" in stead of "if"].
> 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
>
> On Monday, 26 December 2016 09:37:22 UTC, Evertjan. wrote:
>> bart.smissaert@gmail.com wrote on 26 Dec 2016 in comp.lang.javascript:
>>
>> > New to JavaScript and trying to calculate the date of birth from an
>> > Excel date, provided as an integer value (1 Jan 1900 = 1). Having
>> > trouble to get the right syntax for this block of code:
>>
>> There is no "THE right syntax",
>> there is code that does not do what you want it to do,
>> and that does.
>>
>> > If (today_month < M) {
>> > age--;
>> > }
>> > else {
>> > F = parseInt(30.6001 * E);
>> > DOM = (B - (D + F)) + (Q - Z); //day of the month
>>
>> You are talking in riddles, not declaring and not defining variables.
>>
>> > If (DOM > today_day) {
>> > age--;
>> > }
>> > }
>> >
>> > Tried various constructions but no success sofar.
>>
>> You should first define what you want.
>> Then define how you want to do it, perhaps in pseudocode.
>> Then implement it in a way you understand.
>>
>> > Note that the 2 lines
>> > F = .... and DOM = ... only need to run if (today_month < M) is fa
> lse.
>>
>> Lines don't run, they are executed, or not.
>>
>> Try symplified:
>>
>> 1 <script type='text/javascript'>
>> 2 If (true)
>> 3 { alert('first line executes') }
>> 4 else
>> 5 { alert('second line executes') };
>> 6 </script>
>>
>>
>> > I tried with those 2 lines outside the If Else block, but no success
>> > either.
>>
>> Did you try debugging?
>>
>> If so, you should have been aware that 'If' is not 'if'.
>>
>> The Google Chrome console responds with:
>>
>> "Uncaught SyntaxError: Unexpected token else test.html:4"
>>
>> Explanation: the 'else' on line 4 is unexpected because there is no
>> 'if'!
>!!!
>>
>> 'If' should be 'if' as javascript is case-sensitive,
>> so you should think[!] case-sensitive too.
>>
>> > Thanks for any advice.
>>
>> Using code you found on the web,
>> that you don't understand,
>> is bound to fail you,
>> and is bound to keep you in your self-professed newbee-status.
>>
>>
>> --
>> Evertjan.
>> The Netherlands.
>> (Please change the x'es to dots in my emailaddress)
>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2016-12-26 22:14 +0100 |
| Message-ID | <XnsA6EAE239F6C95eejj99@194.109.6.166> |
| In reply to | #31989 |
"Evertjan." <exxjxw.hannivoort@inter.nl.net> wrote on 26 Dec 2016 in
comp.lang.javascript:
> bart.smissaert@gmail.com wrote on 26 Dec 2016 in comp.lang.javascript:
>
>> Verkeerde been uit bed gestapt?
>
> Please do not toppost on usenet.
>
> I can understand you get frustrated,
> when I show you this first and catastrophic mistake
> in your Javascript-code [Using "If" in stead of "if"].
>
>> I have reworked this and this is the full code:
>>
btw, if you still want to use VBS,
consider this [entering now, when still alive]:
function age(born,died)
age = DateDiff("yyyy", born, died) +
(died < DateSerial(Year(died), Month(born), Day(born)))
end function
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.javascript
csiph-web