C# 计算传入的时间距离今天的时间差
程序员文章站
2023-12-15 12:01:16
废话不多说了,直接给大家贴代码了,具体代码如下所示:
///
/// 计算传入的时间距离今天的时间差
///
废话不多说了,直接给大家贴代码了,具体代码如下所示:
/// <summary> /// 计算传入的时间距离今天的时间差 /// </summary> /// <param name="dt"></param> /// <param name="yy"></param> /// <param name="mm"></param> /// <param name="dd"></param> public void getcriminalyx(datetime dt, out int yy, out int mm, out int dd) { datetime now = datetime.now; yy = mm = dd = 0; if (dt.year > 9000 || dt.year == 1900) { return; } if (dt <= now) { return; } stringbuilder str = new stringbuilder(); int dt_y = dt.year; int dt_m = dt.month; int dt_d = dt.day; int now_y = datetime.now.year; int now_m = datetime.now.month; int now_d = datetime.now.day; yy = dt_y - now_y; mm = dt_m - now_m; dd = 0; int dt_m_sy = 0; if (dt_d < now_d) { mm -= 1; dt_m_sy = dt_m - 1; if (dt_m_sy == 0) { dt_m_sy = 12; } if (dt_m_sy == 2) { dt_m_sy = dt_y % 4 == 0 ? 29 : 28; } else { dt_m_sy = dt_m_sy == 2 || dt_m_sy == 4 || dt_m_sy == 6 || dt_m_sy == 9 || dt_m_sy == 11 ? 30 : 31; } dt_d += dt_m_sy; } dd = dt_d - now_d; if (mm < 0) { yy -= 1; mm += 12; } }
总结
以上所述是小编给大家介绍的c# 计算传入的时间距离今天的时间差,希望对大家有所帮助