Reputation: 45
I have created a web application and I'm using Date time and calendar for showing employee time sheet. its run perfectly in my system but now i've published it into the server. In server side it is displaying the following error in datetime calculation or timesheet calculation page.
String was not recognized as a valid DateTime.
I have create whole application with more than 85 pages
. More than 40 pages
having datetime calculation
. i've changed server datetime to system datetime but its not running. The same error occured....
I'm using windows server 2003
Please help me to fix this error ..
Upvotes: 0
Views: 1783
Reputation: 1655
Try This Code:
DateTime DTime = new DateTime();
DTime =Convert.ToDateTime(DateTime.Today.Date.ToShortDateString());
lblDate.Text = string.Format("{0:MM-dd-yy}",DTime);
Upvotes: 2
Reputation: 631
Are you using CultureInfo in convertion? If not try to use Convert.ToDateTime http://msdn.microsoft.com/en-us/library/9xk1h71t(v=vs.90).aspx
Upvotes: 0
Reputation: 5444
If it works on your machine, but not on the server, then I suspect that you are storing culture specific date format (in the database?), and parsing it using the default culture on the server.
CultureInfo ci = new CultureInfo("de-DE");
string germanTime = DateTime.Now.ToString(ci);
DateTime now = DateTime.Parse(germanTime); // Can cause error if the current thread or machine's culture is not German
If you're saving DateTime values in the database, then you should be using CultureInfo.InvariantCulture
to prevent these type of problems. You can also try to configure the culture on the server machine to match the culture format of the date time being parsed, but it's not as robust.
Hopefully, your web application has not ended up storing multiple date formats in your database or else it is going to be a mess.
Upvotes: 0