aldersberegning
hejjeg har fundet følgende script, som kan beregne alder når man indtaster i formatet: ddmmYYYY - da jeg ikke er stærk til javascript ville jeg gerne have lidt hjælp til at man kunne beregne det hvis der blev indtastet et cpr. Ikke en cpr validering, men blot beregning af alder (de sidste 4 skal naturligvis skæres bort vha string-funktionen)
håber det er forståeligt. (alternativt en anden kode hvis nogen ligger inde med den ;-) )
her er koden:
<HTML>
<HEAD>
<SCRIPT language="JavaScript">
<!--
function Tester10(data) {
var result = CalculateAge(data);
document.all.item("age").value = result;
document.all.item("format").value = format;
document.write("jeg er" +result.getyear());
}
-->
</SCRIPT>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<Script language="javascript">
/******************************************************************************
* parameters: date
*
* purpose: Calculates an age for a date
*****************************************************************************/
var format = ""; // Can be Years, Months, Days for age
function CalculateAge(date) {
var slashIndex = date.indexOf("/");
var birthday, birthmonth, birthyear;
if (slashIndex != -1) { // With slashes
var segments = date.split("/");
birthmonth = segments[0];
birthday = segments[1];
birthyear = segments[2];
}
else { // No slashes
birthmonth = date.substring(0, 2);
birthday = date.substring(2, 4);
birthyear = date.substring(4, date.length);
}
var today = new Date();
var thisDay = today.getDate(); // Returns the day of the month
var thisMonth = today.getMonth() + 1; // Need to add a one since the month is zero based - January
var thisYear = today.getFullYear(); // Returns the year
var yearsDiff = CalculateYear(birthyear, thisYear);
var monthsDiff = CalculateMonth(birthmonth, thisMonth);
var daysDiff = CalculateDay(birthday, thisDay);
format = "Years"; // set the format to years
// For the year is greater than zero
if (yearsDiff > 0) {
if (monthsDiff > 0) { // Month is greater than zero so return we have our age
return yearsDiff;
}
else if (monthsDiff < 0) { // Month is less than zero so return the age minus 1
if (yearsDiff != 1) {
return yearsDiff - 1;
}
else {
if (monthsDiff == -11) { // Turn of the year
if (daysDiff >= 0) { // greater than one month
format = "Months";
return 1;
}
else { // less than one month
format = "Days";
return thisDay - (31 - birthday);
}
}
else {
format = "Months"; // Next year but not quite a year old
if (daysDiff >= 0) {
return thisMonth + (12 - birthmonth);
}
else {
return thisMonth + (12 - birthmonth) - 1;
}
}
}
}
else { // We are in the persons birth month
if (daysDiff >= 0) { // Today is greater or equal to the person's birthdate
return yearsDiff;
}
else if (daysDiff < 0 && yearsDiff != 1) { // Today is less than the person's birthdate
return yearsDiff - 1;
}
else {
format = "Months";
return 11;
}
}
}
else { // For the year equaling zero or same year as birthdate
format = "Months";
if (monthsDiff > 0) { // Later in the same year
if (daysDiff >= 0) {
return monthsDiff;
}
else {
if (monthsDiff != 1) {
return monthsDiff - 1;
}
else {
format = "Days";
return thisDay + (DaysInMonth(birthmonth, birthyear) - birthday);
}
}
}
else { // Same month as birth month
format = "Days";
if (daysDiff == 0) {
return 1; // Today's the birthdate
}
else {
return daysDiff;
}
}
}
}
/******************************************************************************
* parameters: birthYear, thisYear
*
* purpose: Calculates the year based on the offsets between the birthyear and
* thisYear
*****************************************************************************/
function CalculateYear(birthYear, thisYear) {
return thisYear - birthYear;
}
/******************************************************************************
* parameters: birthMonth, thisMonth
*
* purpose: Calculates the month based on the offsets between the birthMonth and
* thisMonth
*****************************************************************************/
function CalculateMonth(birthMonth, thisMonth) {
return thisMonth - birthMonth;
}
/******************************************************************************
* parameters: birthday, thisday
*
* purpose: Calculates the month based on the offsets between the birthDay and
* thisDay
*****************************************************************************/
function CalculateDay(birthDay, thisDay) {
return thisDay - birthDay;
}
/******************************************************************************
* parameters: birthday, thisday
*
* purpose: Calculates the days in the month based on month (year for leap year
*
****************************************************************************/
function DaysInMonth(month, year) {
month -= 0;
if (month == 2) {
var leapYear = IsLeapYear(year);
}
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
if (leapYear) {
return 29;
}
else {
return 28;
}
}
}
</Script>
</HEAD>
<BODY>
<P>Alders Beregning:
<INPUT id=text10 name=text10>
<INPUT id=button10 name=button10 type=button value="beregn" onclick="java script:Tester10(document.all.item('text10').value); return true;">
<INPUT type=text name=age id=age disabled>
</P>
</BODY>
</HTML>