Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I need Single Text Box Date Date Format is : "DD/MM/YYYY" in Single text box

Code Is " In Drop Down Code Replace with textbox

XML
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>


    <!-- Script by hscripts.com -->
    <!-- Copyright of HIOXINDIA -->
    <!-- More scripts @www.hscripts.com -->
    <script type="text/javascript">
        var startyear = "1950";
        var endyear = "2010";
        var dat = new Date();
        var curday = dat.getDate();
        var curmon = dat.getMonth() + 1;
        var curyear = dat.getFullYear();
        function checkleapyear(datea) {
            if (datea.getYear() % 4 == 0) {
                if (datea.getYear() % 10 != 0) {
                    return true;
                }
                else {
                    if (datea.getYear() % 400 == 0)
                        return true;
                    else
                        return false;
                }
            }
            return false;
        }
        function DaysInMonth(Y, M) {
            with (new Date(Y, M, 1, 12)) {
                setDate(0);
                return getDate();
            }
        }
        function datediff(date1, date2) {
            var y1 = date1.getFullYear(), m1 = date1.getMonth(), d1 = date1.getDate(),
             y2 = date2.getFullYear(), m2 = date2.getMonth(), d2 = date2.getDate();
            if (d1 < d2) {
                m1--;
                d1 += DaysInMonth(y2, m2);
            }
            if (m1 < m2) {
                y1--;
                m1 += 12;
            }
            return [y1 - y2, m1 - m2, d1 - d2];
        }
        function calage() {
            var calday = document.birthday.day.options[document.birthday.day.selectedIndex].value;
            var calmon = document.birthday.month.options[document.birthday.month.selectedIndex].value;
            var calyear = document.birthday.year.options[document.birthday.year.selectedIndex].value;
            if (curday == "" || curmon == "" || curyear == "" || calday == "" || calmon == "" || calyear == "") {
                alert("please fill all the values and click go -");
            }
            else {
                var curd = new Date(curyear, curmon - 1, curday);
                var cald = new Date(calyear, calmon - 1, calday);
                var diff = Date.UTC(curyear, curmon, curday, 0, 0, 0) - Date.UTC(calyear, calmon, calday, 0, 0, 0);
                var dife = datediff(curd, cald);
                document.birthday.age.value = dife[0] + " years, " + dife[1] + " months, and " + dife[2] + " days";
                var monleft = (dife[0] * 12) + dife[1];
                var secleft = diff / 1000 / 60;
                var hrsleft = secleft / 60;
                var daysleft = hrsleft / 24;
                document.birthday.months.value = monleft + " Month since your birth";
                document.birthday.daa.value = daysleft + " days since your birth";
                document.birthday.hours.value = hrsleft + " hours since your birth";
                document.birthday.min.value = secleft + " minutes since your birth";
                var as = parseInt(calyear) + dife[0] + 1;
                var diff = Date.UTC(as, calmon, calday, 0, 0, 0) - Date.UTC(curyear, curmon, curday, 0, 0, 0);
                var datee = diff / 1000 / 60 / 60 / 24;
                document.birthday.nbday.value = datee + " days left for your next birthday";
            }
        }
    </script>
    <!--     Script by hscripts.com -->

</head>
<body>

    <form name="birthday">

        Date<select name="day" size="1">
            <script type="text/javascript">
                for (var j = 1; j < 32; j++)
                    document.write("<option value=" + j + ">" + j + "</option>");
            </script>
        </select>
        Month<select name="month" size="1">
            <script type="text/javascript">
                for (var i = 1; i < 13; i++)
                    document.write("<option value=" + i + ">" + i + "</option>");
            </script>
        </select>
        Year<select name="year" size="1">
            <script type="text/javascript">
                for (var k = startyear; k < endyear; k++)
                    document.write("<option value=" + k + ">" + k + "</option>");
            </script>
        </select>
        <input name="start" onclick="calage()" value="Calculate" type="button"><br>
        <input name="age" size="40" value="Result"><br>
        You have been living for:<br>

    </form>

</body>
</html>
Posted

Use the following codes.
XML
<div>
            <asp:TextBox ID="txtDateofBirth" runat="server" AutoPostBack="true" OnTextChanged="txtDateofBirth_TextChanged" />
            <span id="lblDateofBirth" runat="server"></span>
            <asp:RequiredFieldValidator ID="ReqDatePicker" ErrorMessage="Please Enter Your Date MM/DD"
                Display="Dynamic" ValidationGroup="v1" SetFocusOnError="true" ForeColor="Red"
                ControlToValidate="txtDateofBirth" runat="server" />
            <asp:Button ID="Btn" Text="Submit" runat="server" />
        </div>



<pre lang="cs">protected void txtDateofBirth_TextChanged(object sender, EventArgs e)
       {
           var DOB = DateTime.ParseExact(txtDateofBirth.Text, &quot;dd/MM/yyyy&quot;, new CultureInfo(&quot;en-US&quot;));
           Age age = new Age(DOB, DateTime.Now);
           lblDateofBirth.InnerText = string.Format(&quot;{0}Years {1} Months and {2}Days&quot;, age.Years, age.Months, age.Days);
       }</pre>



public class Age
    {
        public int Years;
        public int Months;
        public int Days;

        public Age(DateTime Bday)
        {
            this.Count(Bday);
        }

        public Age(DateTime Bday, DateTime Cday)
        {
            this.Count(Bday, Cday);
        }

        public Age Count(DateTime Bday)
        {
            return this.Count(Bday, DateTime.Today);
        }

        public Age Count(DateTime Bday, DateTime Cday)
        {
            if ((Cday.Year - Bday.Year) > 0 ||
                (((Cday.Year - Bday.Year) == 0) && ((Bday.Month < Cday.Month) ||
                  ((Bday.Month == Cday.Month) && (Bday.Day <= Cday.Day)))))
            {
                int DaysInBdayMonth = DateTime.DaysInMonth(Bday.Year, Bday.Month);
                int DaysRemain = Cday.Day + (DaysInBdayMonth - Bday.Day);

                if (Cday.Month > Bday.Month)
                {
                    this.Years = Cday.Year - Bday.Year;
                    this.Months = Cday.Month - (Bday.Month + 1) + Math.Abs(DaysRemain / DaysInBdayMonth);
                    this.Days = (DaysRemain % DaysInBdayMonth + DaysInBdayMonth) % DaysInBdayMonth;
                }
                else if (Cday.Month == Bday.Month)
                {
                    if (Cday.Day >= Bday.Day)
                    {
                        this.Years = Cday.Year - Bday.Year;
                        this.Months = 0;
                        this.Days = Cday.Day - Bday.Day;
                    }
                    else
                    {
                        this.Years = (Cday.Year - 1) - Bday.Year;
                        this.Months = 11;
                        this.Days = DateTime.DaysInMonth(Bday.Year, Bday.Month) - (Bday.Day - Cday.Day);
                    }
                }
                else
                {
                    this.Years = (Cday.Year - 1) - Bday.Year;
                    this.Months = Cday.Month + (11 - Bday.Month) + Math.Abs(DaysRemain / DaysInBdayMonth);
                    this.Days = (DaysRemain % DaysInBdayMonth + DaysInBdayMonth) % DaysInBdayMonth;
                }
            }
            else
            {
                throw new ArgumentException("Birthday date must be earlier than current date");
            }
            return this;
        }
    }
 
Share this answer
 
C#
txtID.Text=string.Format("{0}/{1}/{2},ddldate.Text,ddlmonth.Text,ddlYear.Text);


This above code will also handles memory leakage.

Using jquery,

var text=$('#ddlDate').val();
text+="/";
text+=$('#ddlMonth').val();
text+="/";
text+=$('#ddlYear').val()
$('txtID').val(text);
 
Share this answer
 
v2
Comments
Sai Prasad anumolu 7-Aug-14 5:01am    
kk Thanks I need Textbox Selection only ....Can you Provide any code .....Years is displaying ...Months + Days is noth Displaying sir and i will enter Date in text box ..Like 'DD/MM/YYYY' in text box ..after it will display like this format For example : '25/08/1990' 23 years 11 months 17 days
Strikerzz Arut 7-Aug-14 5:07am    
Do you need to display the age in seperate dropdowns ?
Sai Prasad anumolu 7-Aug-14 5:53am    
Any thing sir i need One example ..... Out put is Like this " 24 years 6 months 8 days "
Strikerzz Arut 7-Aug-14 6:48am    
You want me to do that in javascript or jquery or asp.net?
Sai Prasad anumolu 7-Aug-14 7:00am    
<script type="text/javascript" language="javascript">
function getAge() {
var dob = document.getElementById("txtDateofBirth").value;//get date of birth ebtered in textbox ID txtDOB
var curdate = new Date(); //get Current date
dob = new Date(Date.parse(dob)); //convert dob to javascript date formate
day = curdate.getDate() - dob.getDate();


month = curdate.getMonth() - dob.getMonth();
year = curdate.getFullYear() - dob.getFullYear();

if (day < 0) {
day = curdate.getDate() + 3 - dob.getDate();
month = curdate.getMonth() - 1 - dob.getMonth();
}
if (month < 0) {
month = curdate.getMonth() + 12 - dob.getMonth();
year = curdate.getFullYear() - 1 - dob.getFullYear();
}
// alert("AGE:" + year + " Years," + month + " Months, " + day + " Days."); //Show age in alert


document.getElementById("lblDateofBirth").innerHTML = year + " Years," + month + " Months, " + day + " Days.";//set it to label
}

<div>
<asp:TextBox ID="txtDateofBirth" onblur="getAge()" runat="server" />

<span id="lblDateofBirth"></span>
<asp:RequiredFieldValidator ID="ReqDatePicker" ErrorMessage="Please Enter Your Date MM/DD" Display="Dynamic" ValidationGroup="v1" SetFocusOnError="true" ForeColor="Red" ControlToValidate="txtDateofBirth" runat="server" />
<asp:Button ID="Btn" Text="Submit" runat="server" />
</div>

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