Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #33391 > unrolled thread
| Started by | negisunny38@gmail.com |
|---|---|
| First post | 2017-05-24 09:58 -0700 |
| Last post | 2017-05-24 14:36 -0700 |
| Articles | 19 — 8 participants |
Back to article view | Back to comp.lang.javascript
convert dob into days negisunny38@gmail.com - 2017-05-24 09:58 -0700
Re: convert dob into days "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-05-24 19:16 +0200
Re: convert dob into days negisunny38@gmail.com - 2017-05-24 10:31 -0700
Re: convert dob into days "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-05-24 23:08 +0200
Re: convert dob into days Mark Lloyd <not@mail.invalid> - 2017-05-24 16:33 -0500
Re: convert dob into days "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-05-25 16:29 +0200
Re: convert dob into days Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-24 20:06 +0200
Re: convert dob into days Mark Lloyd <not@mail.invalid> - 2017-05-24 16:24 -0500
Re: convert dob into days Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-25 01:00 +0200
Re: convert dob into days Mark Lloyd <not@mail.invalid> - 2017-05-24 19:35 -0500
Re: convert dob into days Jon Ribbens <jon+usenet@unequivocal.eu> - 2017-05-25 11:27 +0000
Re: convert dob into days Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-25 13:45 +0200
Re: convert dob into days Mark Lloyd <not@mail.invalid> - 2017-05-25 13:35 -0500
Re: convert dob into days Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-26 00:35 +0200
Re: convert dob into days Dr J R Stockton <J.R.Stockton@physics.org> - 2017-05-25 15:45 -0700
Re: convert dob into days "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-05-26 09:12 +0200
Re: convert dob into days Dr J R Stockton <reply1700@merlyn.demon.co.uk.invalid> - 2017-05-26 18:40 +0100
Re: convert dob into days Sam E <why.should.this@be.email.invalid> - 2017-05-27 10:14 -0500
Re: convert dob into days Dr J R Stockton <J.R.Stockton@physics.org> - 2017-05-24 14:36 -0700
| From | negisunny38@gmail.com |
|---|---|
| Date | 2017-05-24 09:58 -0700 |
| Subject | convert dob into days |
| Message-ID | <d839a415-1bb6-485b-a72d-ca9cd4984b52@googlegroups.com> |
Hi
is this a right way to convert dob into days.
========code sample============
//convert date into days
function calculate_age(birth_month, birth_day, birth_year) {
var diffDays = 0;
var oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
var firstDate = new Date(birth_year, birth_month, birth_day);
var secondDate = new Date();
return diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime()) / (oneDay)));
}
I am looking for best way to calculate days lived from birthday.
[toc] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2017-05-24 19:16 +0200 |
| Message-ID | <XnsA77FC419A70Aeejj99@194.109.6.166> |
| In reply to | #33391 |
negisunny38@gmail.com wrote on 24 May 2017 in comp.lang.javascript:
> Hi
>
> is this a right way to convert dob into days.
>
> ========code sample============
>
> //convert date into days
>
> function calculate_age(birth_month, birth_day, birth_year) {
Don't be so American.
> var diffDays = 0;
> var oneDay = 24 * 60 * 60 * 1000; //
> hours*minutes*seconds*milliseconds var firstDate = new
> Date(birth_year, birth_month, birth_day);
1 month wrong!
> var secondDate = new Date();
> return diffDays = Math.round(Math.abs((firstDate.getTime() -
> secondDate.getTime()) / (oneDay)));
>}
>
> I am looking for best way to calculate days lived from birthday.
Nonsense, "best way" is subjective.
Try:
'use strict';
function ageDays(y, m, d) {
let oneDay = 24 * 60 * 60 * 1000;
let dob = new Date(y, m-1, d);
let now = new Date();
return Math.floor( (now-dob)/oneDay );
};
document.write( ageDays(2017,5,22) );
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | negisunny38@gmail.com |
|---|---|
| Date | 2017-05-24 10:31 -0700 |
| Message-ID | <80d4d137-65b6-4a24-b845-a124b6f1156c@googlegroups.com> |
| In reply to | #33393 |
Well i just want to figure out what problem in my function and nothing, also i check this stackoverflow issue https://stackoverflow.com/questions/3224834/get-difference-between-2-dates-in-javascript
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2017-05-24 23:08 +0200 |
| Message-ID | <XnsA77FEB7AD5231eejj99@194.109.6.166> |
| In reply to | #33395 |
negisunny38@gmail.com wrote on 24 May 2017 in comp.lang.javascript: > > Well i just want to figure out what problem in my function and nothing, Nothing what? > also i check this stackoverflow issue > https://stackoverflow.com/questions/3224834/get-difference-between-2-date > s-in-javascript If you respond to something, please quote, this is usenet. -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Mark Lloyd <not@mail.invalid> |
|---|---|
| Date | 2017-05-24 16:33 -0500 |
| Message-ID | <w_mVA.7579$j%.1051@fx03.iad> |
| In reply to | #33393 |
On 05/24/2017 12:16 PM, Evertjan. wrote:
> negisunny38@gmail.com wrote on 24 May 2017 in comp.lang.javascript:
>
>> Hi
>>
>> is this a right way to convert dob into days.
>>
>> ========code sample============
>>
>> //convert date into days
>>
>> function calculate_age(birth_month, birth_day, birth_year) {
>
> Don't be so American.
>
I'm American, and I prefer to put "date parts" in a reasonable order of
decreasing significance (year, month, day) rather than that mixed-up
middle-endian format.
When I write dates for my own use I use that. Today is 2017-5-24.
>> var diffDays = 0;
>> var oneDay = 24 * 60 * 60 * 1000; //
>> hours*minutes*seconds*milliseconds var firstDate = new
>> Date(birth_year, birth_month, birth_day);
>
> 1 month wrong!
How do you know the variable isn't right already? Maybe it's already in
the 0-11 range (0=January, 11=December).
[snip]
--
Mark Lloyd
http://notstupid.us/
"A computer without Microsoft Windows is like chocolate cake without the
mustard."
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2017-05-25 16:29 +0200 |
| Message-ID | <XnsA780A7AE177F1eejj99@194.109.6.166> |
| In reply to | #33402 |
Mark Lloyd <not@mail.invalid> wrote on 24 May 2017 in comp.lang.javascript:
> On 05/24/2017 12:16 PM, Evertjan. wrote:
>> negisunny38@gmail.com wrote on 24 May 2017 in comp.lang.javascript:
>>
>>> Hi
>>>
>>> is this a right way to convert dob into days.
>>>
>>> ========code sample============
>>>
>>> //convert date into days
>>>
>>> function calculate_age(birth_month, birth_day, birth_year) {
>>
>> Don't be so American.
>>
>
> I'm American, and I prefer to put "date parts" in a reasonable order of
> decreasing significance (year, month, day) rather than that mixed-up
> middle-endian format.
Good for you, I bet Trump doesn't. ;-)
> When I write dates for my own use I use that. Today is 2017-5-24.
>
>>> var diffDays = 0;
>>> var oneDay = 24 * 60 * 60 * 1000; //
>>> hours*minutes*seconds*milliseconds var firstDate = new
>>> Date(birth_year, birth_month, birth_day);
>>
>> 1 month wrong!
>
> How do you know the variable isn't right already? Maybe it's already in
> the 0-11 range (0=January, 11=December).
Because people using m-d-y would not understand?
> [snip]
>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2017-05-24 20:06 +0200 |
| Message-ID | <3168351.R56niFO833@PointedEars.de> |
| In reply to | #33391 |
negisunny38@gmail.com wrote:
> is this a right way to convert dob into days.
Not necessarily.
> ========code sample============
>
> //convert date into days
>
> function calculate_age(birth_month, birth_day, birth_year) {
> var diffDays = 0;
> var oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
> var firstDate = new Date(birth_year, birth_month, birth_day);
This is only correct if the month number is passed zero-based. RTFM.
> var secondDate = new Date();
> return diffDays = Math.round(Math.abs((firstDate.getTime() -
> secondDate.getTime()) / (oneDay)));
> }
>
> I am looking for best way to calculate days lived from birthday.
“Birthday” is the day of annual celebration of one’s date of birth.
The proper term here is _birthdate_ or _date of birth_ instead:
<https://en.wikipedia.org/wiki/Birthday>
You should resist the temptation to write code based on local formats.
For example, to me as a European your US American function argument
order looks weird. So does it to the vast majority of this planet’s
population:
<https://en.wikipedia.org/wiki/Date_format_by_country>
Use international standards as basis instead:
<https://en.wikipedia.org/wiki/ISO_8601>
The value of “oneDay” is constant and equivalent to “864e5”. It is
inefficient and unnecessary to calculate it on every call.
Math.abs() becomes unnecessary if you subtract “firstDate” from
“secondDate”. You can then even omit “.getTime()” as the “-” operator
converts its operands to Number (via Date.prototype.valueOf()); but
it would probably be slightly less runtime-efficient.
The assignment in the “return” statement is pointless; so are
the parentheses around “oneDay”. The function returns with
the *right-hand side of the assignment* as value, and the value
assigned to diffDays is discarded upon return.
Your variables are not named wisely. Use “birthDate” instead of
“firstDate”, and “now” instead of “secondDate”.
But, in theory, you do not need any variables here:
/* Assuming month number is passed 1-based */
function calculate_age (birth_year, birth_month, birth_day)
{
return Math.round(
( new Date().getTime()
- new Date(birth_year, birth_month - 1, birth_day).getTime())
/ 864e5
);
}
or
function calculate_age (birth_year, birth_month, birth_day)
{
return Math.round(
( new Date()
- new Date(birth_year, birth_month - 1, birth_day))
/ 864e5
);
}
[As you can see, you would not even need a function here.]
That aside, as you are looking for days and are rounding anyway,
so that DST does not matter, the basic approach is correct.
Next time, post using your real name and not via Google Groups.
Also, lines should not exceed the 80th column; less is better.
--
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Mark Lloyd <not@mail.invalid> |
|---|---|
| Date | 2017-05-24 16:24 -0500 |
| Message-ID | <zSmVA.27368$Qg.7212@fx14.iad> |
| In reply to | #33396 |
On 05/24/2017 01:06 PM, Thomas 'PointedEars' Lahn wrote: [snip] > That aside, as you are looking for days and are rounding anyway, > so that DST does not matter, the basic approach is correct. There is still a possibility of an error here, if you run this calculation late in the day. That could be avoided by setting the time associated with firstDate to noon instead of the default midnight: var firstDate = new Date(birth_year, birth_month, birth_day, 12); [snip] -- Mark Lloyd http://notstupid.us/ "A computer without Microsoft Windows is like chocolate cake without the mustard."
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2017-05-25 01:00 +0200 |
| Message-ID | <3278626.MHq7AAxBmi@PointedEars.de> |
| In reply to | #33400 |
Mark Lloyd wrote: > On 05/24/2017 01:06 PM, Thomas 'PointedEars' Lahn wrote: >> That aside, as you are looking for days and are rounding anyway, >> so that DST does not matter, the basic approach is correct. > > There is still a possibility of an error here, if you run this > calculation late in the day. With rounding? Your From header field value is disregarding Netiquette. -- PointedEars FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix> <https://github.com/PointedEars> | <http://PointedEars.de/wsvn/> Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Mark Lloyd <not@mail.invalid> |
|---|---|
| Date | 2017-05-24 19:35 -0500 |
| Message-ID | <tFpVA.38624$8c1.28455@fx19.iad> |
| In reply to | #33405 |
On 05/24/2017 06:00 PM, Thomas 'PointedEars' Lahn wrote: > Mark Lloyd wrote: > >> On 05/24/2017 01:06 PM, Thomas 'PointedEars' Lahn wrote: >>> That aside, as you are looking for days and are rounding anyway, >>> so that DST does not matter, the basic approach is correct. >> >> There is still a possibility of an error here, if you run this >> calculation late in the day. > > With rounding? Maybe not, but setting the time to noon makes it even less likely. I know some people don't see things that way, but a little work here can prevent an unanticipated error later. > Your From header field value is disregarding Netiquette. > I haven't heard that one before. What about it to you consider undesirable? -- Mark Lloyd http://notstupid.us/ "A computer without Microsoft Windows is like chocolate cake without the mustard."
[toc] | [prev] | [next] | [standalone]
| From | Jon Ribbens <jon+usenet@unequivocal.eu> |
|---|---|
| Date | 2017-05-25 11:27 +0000 |
| Message-ID | <slrnoidg3b.2s0.jon+usenet@sable.unequivocal.eu> |
| In reply to | #33406 |
On 2017-05-25, Mark Lloyd <not@mail.invalid> wrote: > On 05/24/2017 06:00 PM, Thomas 'PointedEars' Lahn wrote: >> Your From header field value is disregarding Netiquette. > > I haven't heard that one before. What about it to you consider undesirable? Ignore him - he complains at everyone about imaginary netiquette violations.
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2017-05-25 13:45 +0200 |
| Message-ID | <1695443.taCxCBeP46@PointedEars.de> |
| In reply to | #33406 |
Mark Lloyd wrote: > On 05/24/2017 06:00 PM, Thomas 'PointedEars' Lahn wrote: >> Mark Lloyd wrote: >>> On 05/24/2017 01:06 PM, Thomas 'PointedEars' Lahn wrote: >>>> That aside, as you are looking for days and are rounding anyway, >>>> so that DST does not matter, the basic approach is correct. >>> There is still a possibility of an error here, if you run this >>> calculation late in the day. >> With rounding? > > Maybe not, but setting the time to noon makes it even less likely. I > know some people don't see things that way, With rounding to integer, as the OP did, it is not possible to get the wrong *number of days*, which the OP was looking for. If DST switches occurred in-between, the calculated difference in milliseconds could be off by at most 3'600'000 (1 standard hour) which amounts to about 0.04167 times the standard day of 86'400'000 milliseconds that they used. | > new Date(2017, 2, 25) | <- Sat Mar 25 2017 00:00:00 GMT+0100 (CET) | | > new Date(2017, 2, 26) | <- Sun Mar 26 2017 00:00:00 GMT+0100 (CET) | | > new Date(2017, 2, 27) | <- Mon Mar 27 2017 00:00:00 GMT+0200 (CEST) | | > new Date(2017, 2, 26) - new Date(2017, 2, 25) | <- 86400000 | | > new Date(2017, 2, 27) - new Date(2017, 2, 26) | <- 82800000 | | > (new Date(2017, 2, 27) - new Date(2017, 2, 26)) / 864e5 | <- 0.9583333333333334 | | > new Date(2017, 9, 28) | <- Sat Oct 28 2017 00:00:00 GMT+0200 (CEST) | | > new Date(2017, 9, 29) | <- Sun Oct 29 2017 00:00:00 GMT+0200 (CEST) | | > new Date(2017, 9, 30) | <- Mon Oct 30 2017 00:00:00 GMT+0100 (CET) | | > new Date(2017, 9, 29) - new Date(2017, 9, 28) | <- 86400000 | | > new Date(2017, 9, 30) - new Date(2017, 9, 29) | <- 90000000 | | > (new Date(2017, 9, 30) - new Date(2017, 9, 29))/864e5 | <- 1.0416666666666667 | | > navigator.userAgent | <- "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) | Chrome/57.0.2987.98 Safari/537.36" > but a little work here can prevent an unanticipated error later. Premature optimization is the root of all evil. Suppose the OP bought into that approach and concluded that this would mean that they would never have to do the rounding, someone in the country of Absurdistan[tm] where clocks are switched at noon (when people would have siesta anyway) would get the wrong value for their age in days if an odd number of DST switches would lie between the dates. >> Your From header field value is disregarding Netiquette. > > I haven't heard that one before. What about it to you consider > undesirable? Use of the “invalid” TLD by you is blocking the private e-mail feedback channel which is an important part of Usenet communication. See also <http://www.interhack.net/pubs/munging-harmful/>. -- PointedEars FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix> <https://github.com/PointedEars> | <http://PointedEars.de/wsvn/> Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Mark Lloyd <not@mail.invalid> |
|---|---|
| Date | 2017-05-25 13:35 -0500 |
| Message-ID | <MtFVA.170554$Dm2.37866@fx44.iad> |
| In reply to | #33408 |
On 05/25/2017 06:45 AM, Thomas 'PointedEars' Lahn wrote:
[snip]
> Premature optimization is the root of all evil. Suppose the OP bought into
> that approach and concluded that this would mean that they would never have
> to do the rounding, someone in the country of Absurdistan[tm] where clocks
> are switched at noon (when people would have siesta anyway) would get the
> wrong value for their age in days if an odd number of DST switches would lie
> between the dates.
If setting the time to noon could cause an error in Absurdistan[tm]
(proximity of DST change to default time), leaving the time set to
midnight could cause an error in any REAL place that does the DST
changes around midnight (some timezones in Europe do).
Also, noon is actually correct. You're specifying a date only, noon
gives you the mean ("average") of that time period.
BTW, I'd choose to still do the rounding. This is NOT an attempt to make
the code shorter or faster, but more correct.
>>> Your From header field value is disregarding Netiquette.
>>
>> I haven't heard that one before. What about it to you consider
>> undesirable?
>
> Use of the “invalid” TLD by you is blocking the private e-mail feedback
> channel which is an important part of Usenet communication.
When I started using usenet, I put my real email address there. I
changed it when that capability was so often abused. I'm not talking
about spam (it comes anyway), but about replies that belong in the
newsgroup. I will provide an E-mail address if necessary. Usually it is not.
> See also <http://www.interhack.net/pubs/munging-harmful/>.
>
--
Mark Lloyd
http://notstupid.us/
"My country is the world, and my religion is to do good." -- Thomas
Paine
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2017-05-26 00:35 +0200 |
| Message-ID | <40438691.fMDQidcC6G@PointedEars.de> |
| In reply to | #33411 |
Mark Lloyd wrote:
> On 05/25/2017 06:45 AM, Thomas 'PointedEars' Lahn wrote:
>> Premature optimization is the root of all evil. Suppose the OP bought
>> into that approach and concluded that this would mean that they would
>> never have to do the rounding, someone in the country of Absurdistan[tm]
>> where clocks are switched at noon (when people would have siesta anyway)
>> would get the wrong value for their age in days if an odd number of DST
>> switches would lie between the dates.
>
> If setting the time to noon could cause an error in Absurdistan[tm]
> (proximity of DST change to default time), leaving the time set to
> midnight could cause an error in any REAL place that does the DST
> changes around midnight (some timezones in Europe do).
You are missing the point.
> Also, noon is actually correct. You're specifying a date only, noon
> gives you the mean ("average") of that time period.
Nonsense.
> BTW, I'd choose to still do the rounding. This is NOT an attempt to make
> the code shorter or faster, but more correct.
You don’t say.
>>>> Your From header field value is disregarding Netiquette.
>>>
>>> I haven't heard that one before. What about it to you consider
>>> undesirable?
>> Use of the “invalid” TLD by you is blocking the private e-mail feedback
>> channel which is an important part of Usenet communication.
>
> When I started using usenet, I put my real email address there. I
> changed it when that capability was so often abused. I'm not talking
> about spam (it comes anyway), but about replies that belong in the
> newsgroup.
What a nonsense. The private feedback channel is there so that subjects can
be discussed in private *because* they do NOT belong in the newsgroup.
> I will provide an E-mail address if necessary.
Not providing an e-mail address causes those replies either be posted to the
newsgroup, where they are off-topic, or it is effectively telling people to
shut up when they would like to tell you something that does not belong
there.
This is *anti-social* behavior by you.
> Usually it is not.
*PLONK*
F'up2 poster
--
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Dr J R Stockton <J.R.Stockton@physics.org> |
|---|---|
| Date | 2017-05-25 15:45 -0700 |
| Message-ID | <eaa19889-7471-4153-a7cc-a80d496fec1d@googlegroups.com> |
| In reply to | #33411 |
On Thursday, May 25, 2017 at 7:35:31 PM UTC+1, Mark Lloyd wrote: > If setting the time to noon could cause an error in Absurdistan[tm] > (proximity of DST change to default time), leaving the time set to > midnight could cause an error in any REAL place that does the DST > changes around midnight (some timezones in Europe do). Only in zones to the West of all of the mainland ones, since European clocks change at 01:00 UTC. Is there actually more than one such zone with use of Summer Time? -- (c) John Stockton, near London, UK. Using Google, no spell-check. | Mail: J.R.""""""""@physics.org |
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2017-05-26 09:12 +0200 |
| Message-ID | <XnsA7815D9D8BE69eejj99@194.109.6.166> |
| In reply to | #33413 |
Dr J R Stockton <J.R.Stockton@physics.org> wrote on 26 May 2017 in comp.lang.javascript: > On Thursday, May 25, 2017 at 7:35:31 PM UTC+1, Mark Lloyd wrote: > > >> If setting the time to noon could cause an error in Absurdistan[tm] >> (proximity of DST change to default time), leaving the time set to >> midnight could cause an error in any REAL place that does the DST >> changes around midnight (some timezones in Europe do). > > Only in zones to the West of all of the mainland ones, since European > clocks change at 01:00 UTC. Is there actually more than one such zone > with use of Summer Time? > All this is missing point of the OQ. If you are comparing a birth-DATE to now-DATETIME the birth-TIME is missing, so the exact number of [totally past?] days in the sense of number of 24- hour periods seems ill-defined. So I would either simply convert now-DATETIME to now-DATE and then compare the 2 dates as UTC/GMT-dates, as that lacks summer-time shift, OR would add the time of birth as an input and specify the "time-zone" of both date-times seperately [as why should they always be the same?]. As mentioned, the simple substraction of two date-times autoconverts to the number of miliseconds difference. -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Dr J R Stockton <reply1700@merlyn.demon.co.uk.invalid> |
|---|---|
| Date | 2017-05-26 18:40 +0100 |
| Message-ID | <3BPEFAZ5hGKZFwIS@invalid.uk.co.demon.merlyn.invalid> |
| In reply to | #33408 |
In comp.lang.javascript message <1695443.taCxCBeP46@PointedEars.de>, Thu, 25 May 2017 13:45:48, Thomas 'PointedEars' Lahn <PointedEars@web.de> posted: >With rounding to integer, as the OP did, it is not possible to get the wrong >*number of days*, which the OP was looking for. If DST switches occurred >in-between, the calculated difference in milliseconds could be off by at >most 3'600'000 (1 standard hour) which amounts to about 0.04167 times the >standard day of 86'400'000 milliseconds that they used. In certain years, the difference between the UK midsummer and midwinter offsets from GMT has been two hours. The root cause of that was the abnormally excessive obnoxiousness of the 'legitimate' government of Germany at the time. In December 2011, two Pacific places omitted one date completely, thereby altering the International Date Line - in effect, a 24-hour change of their offset from GMT. Eastern Kiribati made a 24-hour change in 1995. Alaska once made a 192 hour change. -- (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 | Sam E <why.should.this@be.email.invalid> |
|---|---|
| Date | 2017-05-27 10:14 -0500 |
| Message-ID | <OJgWA.34307$Us3.20433@fx01.iad> |
| In reply to | #33415 |
[snip] > In certain years, the difference between the UK midsummer and midwinter > offsets from GMT has been two hours. Troll station, Antarctica currently has a 2-hour DST offset. [snip]
[toc] | [prev] | [next] | [standalone]
| From | Dr J R Stockton <J.R.Stockton@physics.org> |
|---|---|
| Date | 2017-05-24 14:36 -0700 |
| Message-ID | <e6662e8f-06fc-4bfb-845e-4fa8626e332f@googlegroups.com> |
| In reply to | #33391 |
On Wednesday, May 24, 2017 at 5:58:25 PM UTC+1, negis...@gmail.com wrote:
> is this a right way to convert dob into days.
There may be no unique right answer. Read Poe's
https://en.wikisource.org/wiki/Three_Sundays_in_a_Week.
> ========code sample============
>
> //convert date into days
>
> function calculate_age(birth_month, birth_day, birth_year) {
> var diffDays = 0;
Superfluous assignment.
> var oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
> var firstDate = new Date(birth_year, birth_month, birth_day);
I think that new Date("YYYY-MM-DD") will always nowadays do
what it should; but I have not checked recently.
> var secondDate = new Date();
> return diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime()) / (oneDay)));
> }
The unary + operator applied to a Date Object will return UTC
milliseconds from 1970.0 UTC, ignoring Leap Seconds. For a
similar reason, you can just write (firstDate - secondDate).
I doubt whether Math.abs is needed.
> I am looking for best way to calculate days lived from birthday.
Your use of Math.round will allow for variations in the state of
Summer Time, if the person is currently in his/her natal country
(even, I think, for Russia or the Soviet Union); but it will not
allow for the possibility of being currently in HI although born
in NZ.
If you were calculating the age at death of Methuselah, given
his date of birth, you would, if they had then been invented,
need to consider accumulated Leap Seconds.
Read Mark Lloyd.
--
(c) John Stockton, near London, UK. Using Google, no spell-check. |
Mail: J.R.""""""""@physics.org |
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web