Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #33403

Re: convert dob into days

Newsgroups comp.lang.javascript
Date 2017-05-24 14:36 -0700
References <d839a415-1bb6-485b-a72d-ca9cd4984b52@googlegroups.com>
Message-ID <e6662e8f-06fc-4bfb-845e-4fa8626e332f@googlegroups.com> (permalink)
Subject Re: convert dob into days
From Dr J R Stockton <J.R.Stockton@physics.org>

Show all headers | View raw


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                                      |

Back to comp.lang.javascript | Previous | Next — Previous in thread | Find similar | Unroll thread


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