views:

12487

answers:

3

Does the C# Convert.ToDateTIme function read date as "dd/mm/yyyy" or "mm/dd/yyyy"?

I have the same application on my local machine which I uploaded to my remote shared server. It was working perfectly on my local machine reading "dd/mm/yyyy", but on my remote machine, it seems to read dates as "mm/dd/yyyy". I have the same culture setting "en-GB" on both.

I find this date conversion very unpredictable. Can anyone recommend a culture-proof way of reading date strings from a SQL Server 2005 database?

+2  A: 

Why not simply use the datetime data type? Then, from your sql data reader, use: reader.GetDateTime(int column)

Another option: if you have to pass dates in strings, use the ISO format: yyyy-MM-dd

Chris
+2  A: 

How are your dates actually stored in SQL Server? If they're stored as datetime then you should be able to read them as DateTime values and not need to use Convert.ToDateTime(). Can you show us the query and/or the C# code?

Edit

You threw me off with the mention of SQL Server at the end of your question! Have you considered using a DateTimePicker instead of a TextBox? ASP.NET has date picker controls, right? Then it could do the parsing for you.

Matt Hamilton
i want to convert the datetime from textbox.In my local server i used Convert.toDatetime("MM/dd/yyyy") and this is working .But in uploaded server its not working ....
Avinash
+1  A: 

Well by the sounds of it... One of the settings on the server is off.

I'd go through the "Region & Language" Options with a fine tooth comb and make sure that something isn't override but if that fails.

You could try explicitly setting the Culture Info

         string x = "21/01/2009";

        CultureInfo ci = new CultureInfo("en-GB");

        Convert.ToDateTime(x, ci);
Eoin Campbell