I assume t1
is the month and date and t2
is the hour and minute.
You can use DateTime.ParseExact
:
string t1 = "9/26";
string t2 = "14:51";
DateTime dt = DateTime.ParseExact(string.Format("{0} {1}", t1, t2), "M/dd HH:mm", CultureInfo.InvariantCulture);
TimeSpan diff = DateTime.Now - dt;
double totalHours = diff.TotalHours; // 0.726172292194444
Edit:
I get a error --> not the right format
Maybe because you have unwanted characters at the end of the string(s) as in "14:51\r"
. You can use Trim
or TrimEnd
to remove them. For example:
string t2 = UserParam[13].ToString().TrimEnd(new []{ '\r', '\n' }); // 14:51