Friday, 15 May 2015

c# - how to convert datetime format -


Hello I was giving a text box to the user and Ajax calendar amplifier to select the date in the format dd / mm / yyyy After that I have to use the following function to convert it to the / mm / dd / yyyy format to be inserted in the SQL Server database, but it does not work well in one page, error date time conversion error Mile and other To enter the data, I need to enter yyyy / mm / dd format. My code works fine in the local host in the database, but the errors are coming in the server. I have my work

  protected String (string indicates) {System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo ("en-GB"); Datetime DT Project StartDate = Convert. Date time (inDate); System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo ("en-US"); Return (convert .titime (DT project startup). Ostring ("MM / dd / yyyy")); }  

To add more I used it like getDate_MDY (txtcreatedate.text);

I just yyyy format from the DD / MM / Text box to the correct date in the database. Please show me the correct way to modify it ... Thanks

Use DateTime.Parse Instead of changing the current thread culture, code> or date time.parsect

for example:

  protected string getDate_MDY (string Indiatimes) {DateTime date = DateTime. Pars (indet, new culture info ("n-GB"); Date of return Toastring ("MM / dd / yyyy", New Culture Info ("N-US")); }  

This is not the most effective way of doing this (you probably want to cache CultureInfo cache for the beginning), but what will it be for you? / P>

However, as someone has mentioned in the comment, you should not tell the date of wire (i.e. convert from one culture to another).

Instead of for a SQL command, they should be defined as the date / time parameters in the SQL command:

  (var connection = new SqlConnection (" 

No comments:

Post a Comment