DateTime Conversion issue with C# in asp.net


Hi
So many times we will scenario to convert to string to DateTime while saving in Database and DateTime to string while displaying on textbox of page. If we donot use the exact conversion format then it will create so much problem.

a. String to DateTime Format
Suppose in database I have declared some field as DateTime then I have to pass the input as DateTime only.

objBE.DeliveredDate = DateTime.ParseExact(txtDeliveredDate.Text, “dd/MM/yyyy”, null);

Note: Here objBE.DeliveredDate field is DateTime which have been declared in BE layer.

b. DateTime to String Format

Supposed if we have to fetch the date from database and to display on Textbox then obviously we have to convert into string format. So we can do like this

DateTime aDate = Convert.ToDateTime(dt.Rows[0][“ArrivalDate”]);
txtArrivalDate.Text = aDate.ToString(“dd/MM/yyyy”);

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.