/    Sign up×
Community /Pin to ProfileBookmark

No. of days since birth? user i/p dd, mm, yyyy

I am newbie , please help me. I have hard wired DOB as January 1, 1989, I need program to process it as parameters entered by the user.
function myFunction()
{
var person = prompt(“Please enter your name”, “”);
var day = prompt(“Enter the day of user’s date of birth” );
var month = prompt(“Enter the month of user’s date of birth” );
var year = prompt(“Enter the year of user’s date of birth”);

//test part start-1
var DOB = “January 1, 1989”;
var millisecondsBetweenDOBAnd1970=Date.parse(DOB);
var millisecondsBetweenNowAnd1970=Date.now();
var ageInMilliseconds=millisecondsBetweenNowAnd1970-millisecondsBetweenDOBAnd1970;

var milliseconds=ageInMilliseconds;
var dayy=1000*60*60*24;

var days=Math.round(milliseconds/dayy);
// test part ends

if (person != null)
{
document.getElementById(“demo”).innerHTML =
“” + person + ” ‘s birthday is “+day+” /”+month+” / “+year+” “+days+” days ago”;
}

}

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@cootheadMar 08.2020 — Hi there imkhan,

and a warm welcome to these forums. ;)

You may see a one possible solution here...

https://codepen.io/coothead/full/YzXEoax

...and the actual code used here...

https://codepen.io/coothead/pen/YzXEoax

_coothead_
Copy linkTweet thisAlerts:
@imkhanauthorMar 09.2020 — Thanks coothead. yes. yours is one solution. A good learning opportunity.

In my case I am trying to pass user input:

var person = prompt("Please enter your name", "");

var day = prompt("Enter the day of user's date of birth" );

var month = prompt("Enter the month of user's date of birth" );

var year = prompt("Enter the year of user's date of birth");

so that I can somehow use them in

Date.parse(DOB);
Copy linkTweet thisAlerts:
@imkhanauthorMar 09.2020 — My following code started working (user INPUT: name, year of birth, month of birth, date of birth; OUTPUT: "z 's birthday is 9 / 5 / 1957 23078 ago".

Problem:

- number of days are slightly wrong,

- var DOB = birthYear; this parameter (without month and day) makes no sense but works. What is the right way of doing it?

<script>

function myFunction()

{

const ms_in_a_day=1000*60*60*24;

const ms_in_an_hr=1000*
60*60;

const ms_in_a_mnt=1000*
60;

const seconds =1000;

var userName ="";
var birthYear = 2020;
var birthMonth = 12;
var birthDay=30;
var daysSinceBirthday=0;
var todayDate = new Date();
var birthDate = new Date (birthYear, birthMonth,birthDay);

/* This section collects info from user for name, year of birth, month of birth, and day of birth.
The collected information, updates the values of each variable initialised above.
*/
userName = prompt("Please enter your name e.g. John", "");
birthYear = prompt("Enter the year of user's date of birth e.g. 1998");
birthMonth = prompt("Enter the month of user's date of birth e.g. 8" );
birthDay = prompt("Enter the day of user's date of birth e.g. 27" );



var DOB = birthYear;

var millisecondsBetweenDOBAnd1970=Date.parse(DOB);
var millisecondsBetweenNowAnd1970=Date.now();
var ageInMilliseconds=millisecondsBetweenNowAnd1970-millisecondsBetweenDOBAnd1970;

var milliseconds=ageInMilliseconds;


var daysSinceBirthday=Math.floor(milliseconds/ms_in_a_day);

alert(userName + " 's birthday is " +birthDay+ " / " +birthMonth+ " / " +birthYear+ " " +daysSinceBirthday +" ago ");
}
Copy linkTweet thisAlerts:
@NachfolgerMar 11.2020 — @imkhan#1615942

I have no idea what your function is even trying to do. I'm getting 668 (Months??), when I should be getting 7734 days. On top of the fact that it isn't even correct, the function you provided is insanely bloated. All of that code can be trimmed down into this:
``<i>
</i>function getDOB() {
// Variables
let userName = prompt("Please enter your name e.g. John", "");
let birthYear = prompt("Enter the year of birth (e.g. 1998)");
let birthMonth = prompt("Enter the month of birth (e.g. 8)");
let birthDay= prompt("Enter the day of birth (e.g. 27)");
let birthDate = new Date();
let now = new Date();
let daySpan = 24 * 60 * 60 * 1000;

// Set Day, Month, Year
birthDate.setDate(birthDay);
birthDate.setMonth(birthMonth - 1); // Zero indexed months (0-11, not 1-12)
birthDate.setFullYear(birthYear);

// Output Result
alert(
${userName}'s birthday is ${birthYear}-${birthMonth}-${birthDay}. ${Math.round(Math.abs((now - birthDate) / daySpan))} days ago);
}<i>
</i>
`</CODE>

The above code I provided is way more correct than the one you provided. I'm not even sure what you were calculating, but the code I provided above gives age in days like you asked for.

PS, it will never be 100% accurate because it doesn't account for DST and number of days in a month (Eg. Feb 2020 had 29 days). Also, it's 2020, you use be using <C>
let</C> not <C>var`.

If you want a near perfect solution, use [Moment.js](https://momentjs.com/). That's what I use for all date functions.
Copy linkTweet thisAlerts:
@imkhanauthorMar 13.2020 — thanks all for help. job done
×

Success!

Help @imkhan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.23,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...