Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
System.FormatException: String was not recognized as a valid DateTime.

Source Error:
Line 58: apptime.SlotTime = DateTime.ParseExact(tempdatetime, "dd/MM/yyyy HH:mm:ss tt", culture);


"But it's work when i copy this code to to my local system" ,Error occurred while I running this application from server...pls help me
Posted
Comments
Richard MacCutchan 1-Apr-14 4:05am    
What is the content of the string in tempdatetime?

You should use TryParseExact to check the validity of the tempdatetime value, see example:
C#
using System;
using System.Globalization;

public class Program
{
    public static void Main()
    {

        string tempdatetime = "01/04/2014 01:00:00 AM";
        string format = "dd/MM/yyyy HH:mm:ss tt";
        DateTime dateTime;
        if (DateTime.TryParseExact(tempdatetime, format, CultureInfo.InvariantCulture,
        DateTimeStyles.None, out dateTime))
        {
            Console.WriteLine(dateTime);

        }
        else
        {
            Console.WriteLine("Not a valid date time");
        }
    }
}
 
Share this answer
 
v2
You are telling the system exactly what format to expect, and the data supplied is not in that format.
Now, there are two possibilities here: firstly that the string is not in "dd/MM/yyyy HH:mm:ss tt" format - it may be entered as "1/04/2014 08:09:10 AM" which doesn't exactly match because the "dd" part has only been provided with a single digit.

The second is that the culture is wrong: It is quite possible that you are expecting a culture that matches that on your development machine, and the production server is configured very differently. This could cause the same problem, but is a lot less likely.

Start with the string: look at lit and see exactly what you are trying to convert - then look at where it comes from!
 
Share this answer
 
1.When you are running your application into an running environment, like a web server, or the data comes from a database server, the "current culture info" of that running environment is used and this could be different then your local computer.

2. Because of the running environment, in your case the value from tempdatetime is not in the format that your are trying to convert (maybe the date is in English format like: "mm/dd/yyyy...").

3.If you know directly or indirectly (by using configuration parameter) the "current culture info" of your running environment you should modify your code by first creating the DateTime object from string by using the culture info of your running environment (by using DateTime.Parse(tempdatetime, serverCultureInfo); ), and then convert the resulted DateTime into your custom format.
 
Share this answer
 
v3

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