Click here to Skip to main content
15,905,071 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
DateTime.ParseExact(datetime, @"d/M/yyyy", System.Globalization.CultureInfo.InvariantCulture).ToString();


this code is working in local server .. but not working while uploading can anyone help...
Posted

have you check culture format at your server?

you can set it at application level from web.config file inside system.web tag as:

XML
<system.web>

  <globalization culture="en-GB"/>


the culture "en-GB" supports dd/MM/yyyy format.
for more info about your format check this link

http://www.basicdatepicker.com/samples/cultureinfo.aspx
 
Share this answer
 
Comments
Aswathi Narayan 19-Oct-13 2:21am    
am using en-US and ar-SA in my project... Then how can i make change to this code for proper working???
Probably, the string you are passing does not match that exact format: it may have the year first, or the '/' may be replaced with '-', or it may include leading zeros, or the month may be "APR" or "Oct"

Why are you using Invariant Culture on an international system such as the internet? Surely it would be better to use the Client culture?
C#
Thread.CurrentThread.CurrentCulture = new CultureInfo(Request.UserLanguages[1]);
And then parse the date with a straight DateTime.TryParse?
 
Share this answer
 
Comments
Aswathi Narayan 19-Oct-13 2:27am    
ya.. am passing date in oct. but how can i write this code ????
Thread.CurrentThread.CurrentCulture = new CultureInfo(Request.UserLanguages[1]);

datetime dt= DateTime.TryParse(datetime);
Is this correct????
OriginalGriff 19-Oct-13 3:10am    
No - please, if you don't know how to use a method you should start with Google/MSDN:
http://msdn.microsoft.com/en-us/library/system.datetime.tryparse.aspx
Thread.CurrentThread.CurrentCulture = new CultureInfo(Request.UserLanguages[1]);
datetime dt;
if (!DateTime.TryParse(datetime, out dt))
{
// Report error here
}
else
...

basically, in your original code you are using a format specifier that says "single digit numeric month", and passing it a alphabetic month. Of course it is going to fail! :laugh:
You could use a format that says "alphabetic month":
DateTime.ParseExact(datetime, @"d/MMM/yyyy", System.Globalization.CultureInfo.InvariantCulture)
But on a web site it is a bad idea to assume a date format unless you write it there yourself, and if you do that, you shouldn't be converting it back to a DateTime in the first place :)
People want to enter dates in the format they normally use - so you should respect that unless you want to annoy / confuse your users. And that isn't a good way to get repeat traffic...some cultures are rather xenophobic about these things!

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