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());
In C# there is no Type Date, so all dates is variables of DateTime type. So when you get date from db it transfers into DateTime variable with time 00:00:00
To get string from this you can use ToString("dd-MM-yyyy")
:
var dt = DateTime.Now;
string dateinstring = dt.ToString("dd-MM-yyyy")); //it contains "23-11-2012"
Update:
Your problem is that your datatable is not typed, so your startdate is object, not DateTime. You should cast it to DateTime:
startdate = ((DateTime)dt.Rows[i]["start_date"]).ToString("dd-MM-yyyy");
This will works if your dt.Rows[i]["start_date"]
is not nullable, in other case you should check that it is not null before casting.
You shouldn't worry about the display format of a DateTime in SQL Server. In my SQL management studio they also appear as 2001-12-23, but in my Visual Studio as 23-12-2001.
You probably set the Column Data Type to DateTime
. This adds a time of 00:00:00, if none specified. You can specify the Date
type if you only want to store a date, although it doesn't really matter if you format the DateTime
on your website.
--
Update:
Cast the value from the database to a DateTime, then format it:
DateTime date = Convert.ToDateTime(dt.Rows[i]["start_date"]);
string startdate = date.ToString("dd-MM-yyyy");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.