private string GetSystem()
{
StringBuilder results = new StringBuilder();
DateTimeFormatter[] basicFormatters = new[]
{
// Default date formatters
new DateTimeFormatter("shortdate"),
// Default time formatters
new DateTimeFormatter("longtime"),
};
DateTime dateandTime = DateTime.Now;
foreach (DateTimeFormatter formatter in basicFormatters)
{
// Format and display date/time.
results.Append(formatter.Format(dateandTime));
results.Append(" ");
}
return results.ToString();
}
dateString = GetSystem();
format = "dd-MM-yyyy HH:mm:ss";
provider = new CultureInfo("en-IN");
try
{
result = DateTime.ParseExact(dateString, format, System.Globalization.CultureInfo.InvariantCulture);
Debug.WriteLine("{0} converts to {1}.", dateString, result.ToString());
}
catch (FormatException)
{
Debug.WriteLine("{0} is not in the correct format.", dateString);
}
I'm getting the error
"String was not recognized as a valid DateTime"
while running this code can anyone suggest some idea to resolve my problem.


dateStringexactly? Debug your code and tell us. Also be aware, you are not usingprovideranywhere in your code.GetSystem) a concatenation of ashorttimestring, a space, and alongtimestring. ObviouslyParseExactcan't translate that with a format string ofdd-MM-yyyy HH:mm:ssdd-MM-yyyyis a fairly odd format -yyyy-MM-ddis much more common.