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[“ArrivalDate”]);
txtArrivalDate.Text = aDate.ToString(“dd/MM/yyyy”);