得到两段时间差转为int类型,并作为参数传入下面的方法之中
TimeSpan ts = DataConvert.getTime(Model.outTime).Subtract(DataConvert.getTime(Model.inTime)); int sec = (int)ts.TotalSeconds; string parkMins= parseTimeSeconds(sec);
下面的方法需要传入的参数为秒数也可以通过两段时间相减取得
///<summary> ///由秒数得到日期几天几小时。。。 ///</summary ///<param name="t">秒数</param> ///<param name="type">0:转换后带秒,1:转换后不带秒</param> ///<returns>几天几小时几分几秒</returns> public static string parseTimeSeconds(int t, int type = 1) { string r = ""; int day, hour, minute, second; if (t >= 86400) //天, { day = Convert.ToInt16(t / 86400); hour = Convert.ToInt16((t % 86400) / 3600); minute = Convert.ToInt16((t % 86400 % 3600) / 60); second = Convert.ToInt16(t % 86400 % 3600 % 60); if (type == 0) r = day + ("天") + hour + ("小时") + minute + ("分") + second + ("秒"); else r = day + ("天") + hour + ("小时") + minute + ("分"); } else if (t >= 3600)//时, { hour = Convert.ToInt16(t / 3600); minute = Convert.ToInt16((t % 3600) / 60); second = Convert.ToInt16(t % 3600 % 60); if (type == 0) r = hour + ("小时") + minute + ("分") + second + ("秒"); else r = hour + ("小时") + minute + ("分"); } else if (t >= 60)//分 { minute = Convert.ToInt16(t / 60); second = Convert.ToInt16(t % 60); r = minute + ("分") + second + ("秒"); } else { second = Convert.ToInt16(t); r = second + ("秒"); } return r; }