Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My asp dot net web app is working fine in development machine. but in server date time conversion is failed. error is "string was not recognised as a valid date time".
i am using this code.



DateTime D1 = new DateTime();
                DateTime D2 = new DateTime();
                D1 = DateTime.ParseExact(txtFRLdate.Text, "dd/MM/yyyy", CultureInfo.CreateSpecificCulture("en-GB"));
                D2 = Convert.ToDateTime(txtFRLdate.Text, System.Globalization.CultureInfo.GetCultureInfo("hi-IN"));


I have set server language "Eng India" but its not working.

What I have tried:

DateTime D1 = new DateTime();
               DateTime D2 = new DateTime();
               D1 = DateTime.ParseExact(txtFRLdate.Text, "dd/MM/yyyy", CultureInfo.CreateSpecificCulture("en-GB"));
               D2 = Convert.ToDateTime(txtFRLdate.Text, System.Globalization.CultureInfo.GetCultureInfo("hi-IN"));


DateTime.ParseExact(txtFRLdate.Text, "dd/MM/yyyy", CultureInfo.CreateSpecificCulture("en-GB"));
Posted
Updated 13-Jun-20 4:38am
Comments
F-ES Sitecore 13-Jun-20 10:41am    
It's generally not a good idea to rely on the settings on the server, instead you should just code it such that it works regardless of server settings, so always use the appropriate culture when converting text.
Asif 7969814 13-Jun-20 10:46am    
CultureInfo.CreateSpecificCulture("en-GB")); is not working in server so what should i do.
F-ES Sitecore 14-Jun-20 9:08am    
You haven't said what the input is though, that's the important thing. As OriginalGriff said, if you let people input any format date then it gets complicated, you have to tell them what format to enter, using validation to ensure they do, or use a datepicker or three different inputs for day\month\year to remove ambiguity.

That's a text box: you have no control over what the user inputs to it. It may be a valid date, it may not.
So don't use ParseExact, use TryParseExact instead, report any problems to the user and let him correct it before trying again.
The user should be allowed to enter dates in any format, particularly in a web app where you have no idea where the user is and what format he might enter dates - it's your job to find out what date format his machine is set to and accept those!

It is much better to not assume that the date on the system is in any format, or that the user will input a data in any format you ask him to - so don't use a textBox, use a Calendar control instead and use the DateTime value that he enters there instead - it will be in a "neutral" internal format of "ticks since a fixed date and time in the past" instead of days months and years and will work on any system.
 
Share this answer
 
Its very easy install English India in language. go to date setting make server short date dd-mm-yyyy

then go to IIS service manager click on your app or default web site click on .net globalisation, set culture_info_united_kingdom(en-gb). and it will work fine.

thanks for all your help
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900