I have a SQL Server database which has a column of date with value of 2001-12-23. The insert statement makes sure that the input is correctly formatted using the following code:
var start_date_formatted = DateTime.ParseExact(start_date.Text, "dd-MM-yyyy", null);
Even though the input is formatted as dd-MM-yyyy it still appears in the database as 2001-12-23.
The thing that I don't understand is the fact that whenever I query the data from the database, it returns the date correctly formatted dd-MM-yyyy even though it appears in the database as yyyy-MM-dd.
That is not a big deal since I got the formatting I need eventually.
The problem is that the time string is being added to my date generating the output of 23-12-2001 00:00:00. I need date only but can't figure out why is it being added and why can't I change it using the following method:
DateTime.ParseExact(startdate, "dd-MM-YYYY", null); // string is not recognized as valid DateTime
or
.ToString("dd-MM-YYYY"); // no overload method ToString that takes 1 argument
Since I'm new to asp.net I would like to get to know how to handle date formats and if there is any general config setting which would require and set certain date format?
UPDATE
var dt = BookingAccess.ManageBookingsDataTable();
string id = string.Empty;
string name = string.Empty;
string startdate = string.Empty;
string enddate = string.Empty;
string full_string = string.Empty;
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
id = dt.Rows[i]["id"].ToString();
var sdate = dt.Rows[i]["start_date"];
name = dt.Rows[i]["Pet name"].ToString();
startdate = dt.Rows[i]["start_date"].ToString("dd-MM-yyyy"); // error is thrown here
enddate = dt.Rows[i]["end_date"].ToString();
full_string = startdate + " to " + enddate + " (" + name + ")";
CurrentBookings.Items.Add(new ListItem(full_string, id));
}
}
This does not seem to format the date either:
startdate = string.Format("{0:dd/MM/yyyy}", dt.Rows[i]["start_date"].ToString());