Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,

How i get Birthdate using age in javascript or asp.net.

Thanks in Advance
Posted

try like this

C#
protected void Button1_Click(object sender, EventArgs e)
  {
      int year = DateTime.Now.Year;
      int age = Convert.ToInt32(TextBox1.Text);
      int dobyear = year - age;
      int  date = DateTime.Now.Day ;
      int agedate = 0;//here enter days
      int dobdate = date - agedate;
      int month = DateTime.Now.Month;
      int agemonth = 0;//enter months
      int dobmonth = month - agemonth;
      Response.Write("Your Date Of Birth Is" + dobyear + "  " + dobmonth + "  " + dobdate);


  }
 
Share this answer
 
Hi Frnds,

Thanks For your reply
I solved it My self using javascript

Javscript Code

JavaScript
function GetBirthDate() {
            var yr = document.getElementById("txtYear").value.replace(/^\s+|\s+$/g, "");
            var mn = document.getElementById("txtMonth").value.replace(/^\s+|\s+$/g, "");
            var dy = document.getElementById("txtDay").value.replace(/^\s+|\s+$/g, "");
            if (dy < 10) { dy = '0' + dy } if (mn < 10) { mn = '0' + mn }
            alert(new Date(new Date().getFullYear() - yr, new Date().getMonth()  - mn , (new Date().getDay()+3)-dy));
        }


XML
<input id="txtYear" onkeyup="GetBirthDate()" type="text" />
    <input id="txtMonth" onkeyup="GetBirthDate()"type="text" />
    <input id="txtDay" onkeyup="GetBirthDate()" type="text" />


Thank you
 
Share this answer
 
JS:Function

C#
//Calculate Age (DOB)-----------------------------------------------------------------------
function ValidateFormNew(inputFieldId, outputFieldId)
{
    var age;
    var input = document.getElementById(inputFieldId).value;
    var pyear = parseInt(input.substring(6,10));
    var pmonth = parseInt(input.substring(3,5)) - 1;
    var pday = parseInt(input.substring(0,2));
    today = new Date();
    year = today.getFullYear() ;
    month = today.getMonth();
    day = today.getDate();
    if ( month < pmonth )
    {
        age = year - pyear - 1;
    }
    else if ( month > pmonth )
    {
        age = year - pyear;
    }
    else if ( month == pmonth )
    {
        if ( day < pday )
        {
            age = year - pyear - 1;
        }
        else if ( day > pday )
        {
            age = year - pyear;
        }
        else if ( day == pday )
        {
            age = year - pyear;
        }
    }
    if (age != undefined)
    {
        document.getElementById(outputFieldId).value = age;
    }
    ValidateForm(inputFieldId);
}


-------------------------------------------------


Server Side:

TxtMDOB.Attributes.Add("Onfocusout", "ValidateFormNew('" & TxtMDOB.ClientID & "','" & TxtMAge.ClientID & "');")
 
Share this answer
 
Comments
Nilesh Patil Kolhapur 5-Jun-12 2:39am    
Hello Jimmy i dont want age i want birthdate using age

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900