This repository was archived by the owner on Feb 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathageCalculator.js
65 lines (55 loc) · 1.74 KB
/
ageCalculator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
export default ageCalculator
/**
* Original Source: https://stackoverflow.com/a/7091965
*
* This method will calculate your today's age from
* your date of birth
*
* @param {String} dateString - The birthdate String in the form (dd/mm/yyyy)
* @return {String} - today's age String
*/
//is valid date format
function ageCalculator (dateString) {
if (!isDate(dateString))
return "Not a valid date";
var birthDate = parseDate(dateString);
var today = new Date();
var age = today.getFullYear() - birthDate.getFullYear();
var age_months = today.getMonth() - birthDate.getMonth();
var age_days = today.getDate() - birthDate.getDate();
if (age_months < 0 || (age_months == 0 && age_days < 0)) {
age = parseInt(age) - 1;
}
return age;
}
//convert the date string in the format of dd/mm/yyyy into a JS date object
function parseDate(dateString) {
var dateParts = dateString.split("/");
return new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);
}
function isDate(txtDate) {
var currVal = txtDate;
if (currVal == '')
return true;
//Declare Regex
var rxDatePattern = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
var dtArray = currVal.match(rxDatePattern); // is format OK?
if (dtArray == null)
return false;
//Checks for dd/mm/yyyy format.
var dtDay = dtArray[1];
var dtMonth = dtArray[3];
var dtYear = dtArray[5];
if (dtMonth < 1 || dtMonth > 12)
return false;
else if (dtDay < 1 || dtDay > 31)
return false;
else if ((dtMonth == 4 || dtMonth == 6 || dtMonth == 9 || dtMonth == 11) && dtDay == 31)
return false;
else if (dtMonth == 2) {
var isleap = (dtYear % 4 == 0 && (dtYear % 100 != 0 || dtYear % 400 == 0));
if (dtDay > 29 || (dtDay == 29 && !isleap))
return false;
}
return true;
}