html - How to calculate age in JavaScript with the format ddmmyyyy? - Stack Overflow

How to take user input in the ddmmyyyy format and use it to calculate the user's age?function us

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
Add a ment  | 

3 Answers 3

Reset to default 3

We can solve this in two ways:

  1. 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);

  1. 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信