I have a string variable with it's value formatted to look like "1:23:45 AM" I need to convert that into a DateTime variable for time without a date component. I am receiving an "Unhandled exception" error that is being caused by the formatting. If I remove the "AM", it works fine but the "AM/PM" component is needed for what I am trying to do. I am currently using the following code to attempt this...
"temp" is the name of the variable I am using until I come up with a more meaningful variable name...
public string PlusMinus12(string hour, string minute, string second, string ampm)
{
string PlusMinus;
int rndmTmp1 = Random1();
int rndmTmp2 = Random2();
if (rndmTmp1 == 0)
{
PlusMinus = hour + ":" + minute + ":" + second + ": " + ampm;
return PlusMinus;
}
else if (rndmTmp1 == 1)
{
string temp = hour + ":" + minute + ":" + second +": " + ampm;
**DateTime subtract = DateTime.Parse(temp);**
subtract.AddSeconds(-rndmTmp2);
PlusMinus = subtract.ToString("h:mm:ss tt");
return PlusMinus;
}
else
{
DateTime subtract = DateTime.Parse(temp); is the line causing the error
The error is:
An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll
Additional information: String was not recognized as a valid DateTime.
Most of the information I have found so far on this topic include solutions that depend on the existence of the Date component, which I am not using.
Any ideas? Thank you