I'm trying below code to try to parse time and add it to current date:
string[] sDatetimeFormat1 = { "M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt",
"MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss",
"M/d/yyyy hh:mm tt", "M/d/yyyy hh tt",
"M/d/yyyy h:mm", "M/d/yyyy h:mm",
"MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm" };
string[] sDatetimeFormat2 = { "HH:mm tt", "h:m t", "HH:mm:ss",
"HH:mm:ss tt", "h:m:s t"};
if (DateTime.TryParseExact(InputDateOrTime.Replace("\"", ""),
sDatetimeFormat1, new CultureInfo("en-US"), DateTimeStyles.None,
out sStartTime))
{
//something
}
else if (TimeSpan.TryParse(InputDateOrTime.Replace("\"", ""), out tsInput))
{
sStartTime = DateTime.Parse(DateTime.Today.ToString() + tsInput);
}
Input might appear as in sDateTimeFormat1 or might as well appear as in sDatetimeFormat2, If it's sDatetimeFormat1 then I need to just parse it. If it's as in sDatetimeFormat2, then I need to add that time to current date.
Ex: "06/03/2016 14:22:00" should be parsed as "06/03/2016 2:22:00 PM"
"2:22 PM" should be parsed as "06/03/2016 2:22:00 PM" (i.e., today + timespan: format is not important, but it should represent the same date-time).
.Net version is 2.0