Click here to Skip to main content
15,885,953 members
Please Sign up or sign in to vote.
1.73/5 (4 votes)
See more:
Hi,

I am getting an error of string converion to datetime.

strdate comes dynamically..for e.g: strdate11=24-06-12
C#
dtDate12 = Convert.ToDateTime(strdate11);

the above was working in my localhost; but gave error on staging site.

So, I wrote the conversion format in the folloeing way:
C#
dtDate12 = DateTime.ParseExact(strdate11, "MM/dd/yyyy HH:mm:ss", null);

and also the last attribute to CultureInfo.InvariantCulture;

But it gives me eror on my localhost itself:
String was not recognized as a valid DateTime

Please help

Thanks,
Kiran
Posted
v2
Comments
[no name] 9-Jul-12 9:09am    
Additionally, I think that you will probably find that "12" is not a valid year.

This is due to the difference in culture settings on your local machine and hosting server. You need to specify the culture in your web.config which will override the hosting server settings

http://stackoverflow.com/questions/3768061/datetime-format-different-on-local-machine-compared-to-production-machine[^]
 
Share this answer
 
try This :
IFormatProvider theCultureInfo = new System.Globalization.CultureInfo("en-GB", true);
     DateTime from_date = DateTime.ParseExact(string1, "dd/MM/yyyy", theCultureInfo);
     DateTime to = DateTime.ParseExact(string2, "dd/MM/yyyy", theCultureInfo);

this will convert your string into Datetime Object.

Hope this will help you.
 
Share this answer
 
v2
Try this...its working
C#
string dateString = "01-01-2012";
        DateTime date1 = new DateTime();
        date1 = DateTime.Parse(dateString, new CultureInfo("en-GB").DateTimeFormat);
 
Share this answer
 
Change this (year format change)
dtDate12 = DateTime.ParseExact(strdate11, "MM/dd/yy HH:mm:ss", null);
 
Share this answer
 
you can also use :-
Quote:
DateTime dtEx = DateTime.Parse(str);
 
Share this answer
 
If the string is questionable do a try parse to test before you parse the data from the string, you should be able to reuse the original code if the try parse returns the correct value else you need to format your date string correctly.
 
Share this answer
 
hi
Check this[^]

try this[^]
may be this will help you.

Thank You
ChetanV
 
Share this answer
 
v2

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