Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a form in which i have a field date of birth , and a textbox to fill date of birth , when a person fills his date of birth like 01/01/1990 , then it will be saved into database , and when i retrieve this date form database then this comes 01/01/1990 12:00 AM , i want only date part from it.


This is my form to fillup date of birth

form.aspx
XML
 <tr>
    <td width="30%">
        Date of Birth
        <br />
    </td>
    <td style="width: 1px">
       :<asp:TextBox ID="txtDateofBirth" runat="server" placeholder="dd/mm/yyyy"></asp:TextBox>
    </td>
    <td class="td2">
    </td>
</tr>


This is code for form.aspx.cs
C#
SqlCommand cmd = new SqlCommand(
"insert into ApplicantForm(NameofApplicant, dateofbirth)"
+ "values( @NameofApplicant, convert(Datetime, @dateofBirth, 103)", con);

cmd.Parameters.AddWithValue("@dateofBirth",txtDateofBirth.Text);



this is code for print of this date of birth
print.aspx
XML
<asp:Label ID="txtDOB" runat="server"  ></asp:Label>


this is code print.aspx.cs
C#
SqlCommand cmd = new SqlCommand(
"Select * from ApplicantForm 
Where ApplicantId=" + ((Request.QueryString["ApplicantId"])) + "", con);

dr = cmd.ExecuteReader();

txtDOB.Text = dr["dob"].ToString();
Posted
Updated 9-Jun-21 7:08am
v2

Modify as below



txtDOB.Text = dr["dob"].ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);



Make sure to add System.Globalization namespace when using CultureInfo;
 
Share this answer
 
v4
Comments
Marcin Kozub 27-Nov-14 2:15am    
txtDOB.Text = dr["dob"].ToShortDateString();
Which will return date with current culture format.
Solution No.1: If u r using the SQl Server 2008 and above Database U can use the Datatype
as Date.

Solution No.2: U can use the convert(varchar(10),GETDATE(), 105)
convert(varchar(10),GETDATE() , 103)

Solution No.3: Format(dr["dob"], "dd-MM-yyyy").ToString try this format function it will useful
 
Share this answer
 
DateTime dt = Convert.ToDateTime(dr["DateOfJoining"].ToString());
txtDOB.Text = string.Format("{0}/{1}/{2}", dt.Month, dt.Day, dt.Year);
 
Share this answer
 
Comments
Richard Deeming 10-Jun-21 4:33am    
Solution 1 already showed how to correctly format a DateTime value. Converting a DateTime to a String, back to a DateTime, and then incorrectly formatting it does not add anything to the discussion.

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