简体   繁体   中英

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

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?

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);

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

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.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM