How to take user input in the dd/mm/yyyy format and use it to calculate the user's age?
function userAge() {
var todayDate = new Date().getMilliseconds;
var birthDateSplit = document.getElementById("birthDate").split('/');
var birthDate = new Date(birthDateSplit[2],birthDateSplit[1],birthDateSplit[0]);
var day = birthDate[2]*86400000;
var month = birthDate[1]*2629746000;
var year = birthDate[0]*31556952000;
var age = today - (day + month + year);
return age;
}
How to take user input in the dd/mm/yyyy format and use it to calculate the user's age?
function userAge() {
var todayDate = new Date().getMilliseconds;
var birthDateSplit = document.getElementById("birthDate").split('/');
var birthDate = new Date(birthDateSplit[2],birthDateSplit[1],birthDateSplit[0]);
var day = birthDate[2]*86400000;
var month = birthDate[1]*2629746000;
var year = birthDate[0]*31556952000;
var age = today - (day + month + year);
return age;
}
Share
edited Jun 4, 2019 at 19:18
username653ue6
asked Apr 24, 2019 at 18:24
username653ue6username653ue6
312 silver badges10 bronze badges
5
-
1
What is in
today
? – Scott Hunter Commented Apr 24, 2019 at 18:25 -
You probably men
.getElementById("birthDate").value.split(...)
– Pointy Commented Apr 24, 2019 at 18:26 - Look at the developer tools.... It will tell you why it returns nothing. There are errors. Learn to debug. – epascarello Commented Apr 24, 2019 at 18:31
- @thezeroandone The issue is the code above does not work because you have errors in it. The developer console should be telling you about the errors. Open up the developer console in the browser and look at the error messages. (and yes, the way you are doing it is flawed) – epascarello Commented Apr 24, 2019 at 18:39
- @thezeroandone ah ah I see, sorry the ma threw me off :) – Pointy Commented Apr 24, 2019 at 18:44
3 Answers
Reset to default 3We can solve this in two ways:
- You can use javascript dates if you don't want to go for plugins.
Take value from the field, and formulate it as a date.
var birthDayDate = document.getElementById("birthDate").value;
var from = birthDayDate.split("/");
var birthdateTimeStamp = new Date(from[2], from[1] - 1, from[0]);
Code Sample:
var from = "15/09/1994".split("/");
var birthdateTimeStamp = new Date(from[2], from[1] - 1, from[0]);
var cur = new Date();
var diff = cur - birthdateTimeStamp;
// This is the difference in milliseconds
var currentAge = Math.floor(diff/31557600000);
// Divide by 1000*60*60*24*365.25
console.log(currentAge);
- You can do it with moment.js in a single line - you have cdn & npm for it.
var birthDayDate = document.getElementById("birthDate").value;
const ageInYears = moment().diff(new Date(birthDayDate), 'years');
console.log(ageInYears);
const years = moment().diff(new Date('1981/01/01'), 'years');
console.log(years);
<script src="https://cdnjs.cloudflare./ajax/libs/moment.js/2.24.0/moment.min.js"></script>
The Date
constructor will handle parsing your user input. Then you can use getTime()
to get the millisecond time.
function userAge() {
var todayDate = new Date().getTime();
var birthDate = new Date(document.getElementById("birthDate")).getTime();
var age = (todayDate - birthDate) / (1000 * 60 * 60 * 24 * 365)
return age;
}
The age is current_year - yyyy.
Then -1 if the mm>current_month or mm==current_month and dd>current_day.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744908366a4600394.html
评论列表(0条)