0

I have a database table with a column Birthdate. I want to display the date from the db table to a 'date' textfield on my webform.

For example, the data in my database table Birthdate column is 'Sep 18, 2000' and I want to display it on the 'date' textfield with a format of dd/mm/yyyy.

This is what I have did but it does not display anything on the 'date' textfield

Code in Webform1.aspx.cs

string date = DataAccess.BrgyCitizenBirthdate;
DateTime getBDate = Convert.ToDateTime(DateTime.ParseExact(date, "MMM dd, yyyy", null));
BCBirthdate = getBDate.ToString("dd/mm/yyyy");

Note that the value of DataAccess.BrgyCitizenBirthdate; is Sep 18, 2000.

Code in Webform2.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
   txtBirthdate.Text = Brgy_Citizens.BCBirthdate;
}

My output

enter image description here

My DESIRED output

enter image description here

1
  • Use the Convert.ToDateTime method to convert the date display format: Convert.ToDateTime("2012-12-21").ToString("yyyy/MM/dd"). Commented Sep 14, 2022 at 6:29

1 Answer 1

1

Use this:

txtBirthdate.Text = Brgy_Citizens.BCBirthdate.ToShortDateString();

You don't really want to use fixed formatting, since that may well change.

But, as a FYI?

this would work (it is MM, and not mm)

getBDate.ToString("dd/MM/yyyy");

Well, you kind have to make a choice here.

that date setting is going to be the result of regional settings on the client side.

And you can well see the default settings - just look at the default date setting when you drop in that text box + textmode = date.

So, for the simple answer?

Shove into that text box a date formatted to what the text box is expecting.

I thus suggest using ToShortDateString, and it should work if the date settings are different or changed on the web server.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.