0

I have a web application using visual studio 2013. I have connected SQL server 2008 with this web application. The user inputs a date with a format of

yyyy-mm-dd

. Then this is sent to sql server. When I open sql server and view my database the entry for date reads as yyyy-mm-dd.

I have another page on my web application that calls data from the database and populates the web form. When it populates the date field it is populated as mm/dd/yyyy 12:00:00 AM. As far as I can tell I have all of my data types set to date.

I have reached the data types for sql and html. What I found is the format should just be a date with no time.

Ex.) yyyy-mm-dd

I also read through this question How to return the date part only from a SQL Server datetime datatype but I am not sure how to apply it to my problem. I am not using data type 'datetime'. Any suggestions or help would be greatly appreciated!

Edit: When I run my web application I have added a breakpoint to see what value the program is reading from the database and it says yyyy-mm-dd 12:00:00 AM. I don't know if this will help figure things out or not.

Community
  • 1
  • 1
Guy Hendrickson
  • 87
  • 1
  • 1
  • 8
  • SQL Server sounds fine so don't go in circles there. It has to be the date control you're using on the web page. It sounds like it has a specific format set of US date and time - this could even be a default that you have to override. – Janine Rawnsley Jun 17 '14 at 16:05
  • may be the answers here can help you http://stackoverflow.com/questions/15336272/how-to-get-only-the-date-excluding-time-in-asp-net-c-sharp – Karthik Ganesan Jun 17 '14 at 16:21
  • @JanineRawnsley do you have any suggestions of how to override the default format? – Guy Hendrickson Jun 17 '14 at 16:23
  • How are you retrieving data from the database? You can generally set a custom format on date controls. e.g. .net formats are here http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx – Janine Rawnsley Jun 17 '14 at 16:29
  • @JanineRawnsley I am sending putting my data into a dataset and then setting putting the data from the dataset into a data table. I then read the data from the data table like this 'dteHireDate.Value = SecondTable.Rows[0]["HireDate"].ToString();' – Guy Hendrickson Jun 17 '14 at 16:40
  • @KarthikGanesan I followed you link and I was able to get in some what working. The format is right but it is not centered on the input box. This is what my code looks like now 'DateTime dateOnly2 = Convert.ToDateTime(SecondTable.Rows[0]["HireDate"].ToString()); dteHireDate.Value = dateOnly2.ToString("yyyy-MM-dd");'. Thanks for the help! – Guy Hendrickson Jun 17 '14 at 16:41
  • In your ToString() try a custom format, e.g. ToString("dd/mm/yyyy"). Alternatively see if dteHireDate has a property on it to allow specific formats e.g. dteHireDate.Format – Janine Rawnsley Jun 17 '14 at 16:43
  • @JanineRawnsley I was not able to format it in my original statement but when I changed it to the datetime I was able to set the format. Thanks for your help! – Guy Hendrickson Jun 17 '14 at 16:54
  • See answer below.. should work on one line of code so long as you cast your field to a datetime. – Janine Rawnsley Jun 17 '14 at 16:57

1 Answers1

0

Force the date format you want in your control e.g.

yourDateControl.Value = Convert.ToDateTime(Datatable.Rows[0]["Field"].ToString("dd/MM/yyyy"))
Janine Rawnsley
  • 1,240
  • 2
  • 10
  • 20