Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #33396
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: convert dob into days |
| Date | 2017-05-24 20:06 +0200 |
| Organization | PointedEars Software (PES) |
| Message-ID | <3168351.R56niFO833@PointedEars.de> (permalink) |
| References | <d839a415-1bb6-485b-a72d-ca9cd4984b52@googlegroups.com> |
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.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web