Path: csiph.com!news.freedyn.net!news.mb-net.net!open-news-network.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: convert dob into days Date: Wed, 24 May 2017 20:06:20 +0200 Organization: PointedEars Software (PES) Lines: 92 Message-ID: <3168351.R56niFO833@PointedEars.de> References: Reply-To: Thomas 'PointedEars' Lahn NNTP-Posting-Host: 80.227.197.178.dynamic.wless.zhbmb00p-cgnat.res.cust.swisscom.ch Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: gwaiyur.mb-net.net 1495649182 2461 178.197.227.80 (24 May 2017 18:06:22 GMT) X-Complaints-To: abuse@open-news-network.org NNTP-Posting-Date: Wed, 24 May 2017 18:06:22 +0000 (UTC) User-Agent: KNode/4.14.2 X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. 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: 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: Use international standards as basis instead: 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: | | Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.