计算两个日期相差几年几个月
public static class dateTimeDiff
{
/// <summary>
/// 计算日期间隔
/// </summary>
/// <param name="d1">要参与计算的其中一个日期字符串</param>
/// <param name="d2">要参与计算的另一个日期字符串</param>
/// <returns>一个表示日期间隔的TimeSpan类型</returns>
public static TimeSpan toResult(string d1, string d2)
{
try
{
DateTime date1 = DateTime.Parse(d1);
DateTime date2 = DateTime.Parse(d2);
return toResult(date1, date2);
}
catch
{
throw new Exception("字符串参数不正确!");
}
}
/// <summary>
/// 计算日期间隔
/// </summary>
/// <param name="d1">要参与计算的其中一个日期</param>
/// <param name="d2">要参与计算的另一个日期</param>
/// <returns>一个表示日期间隔的TimeSpan类型</returns>
public static TimeSpan toResult(DateTime d1, DateTime d2)
{
TimeSpan ts;
if (d1 > d2)
{
ts = d1 - d2;
}
else
{
ts = d2 - d1;
}
return ts;
}
/// <summary>
/// 计算日期间隔
/// </summary>
/// <param name="d1">要参与计算的其中一个日期字符串</param>
/// <param name="d2">要参与计算的另一个日期字符串</param>
/// <param name="drf">决定返回值形式的枚举</param>
/// <returns>一个代表年月日的int数组,具体数组长度与枚举参数drf有关</returns>
public static int[] toResult(string d1, string d2, diffFormat drf)
{
try
{
DateTime date1 = DateTime.Parse(d1);
DateTime date2 = DateTime.Parse(d2);
return toResult(date1, date2, drf);
}
catch
{
throw new Exception("字符串参数不正确!");
}
}
/// <summary>
/// 计算日期间隔
/// </summary>
/// <param name="d1">要参与计算的其中一个日期</param>
/// <param name="d2">要参与计算的另一个日期</param>
/// <param name="drf">决定返回值形式的枚举</param>
/// <returns>一个代表年月日的int数组,具体数组长度与枚举参数drf有关</returns>
public static int[] toResult(DateTime d1, DateTime d2, diffFormat drf)
{
#region 数据初始化
DateTime max;
DateTime min;
int year;
int month;
int tempYear, tempMonth;
if (d1 > d2)
{
max = d1;
min = d2;
}
else
{
max = d2;
min = d1;
}
tempYear = max.Year;
tempMonth = max.Month;
if (max.Month < min.Month)
{
tempYear--;
tempMonth = tempMonth + 12;
}
year = tempYear - min.Year;
month = tempMonth - min.Month;
#endregion
#region 按条件计算
if (drf == diffFormat.Day)
{
TimeSpan ts = max - min;
return new int[] { ts.Days };
}
if (drf == diffFormat.Month)
{
return new int[] { month + year * 12 };
}
if (drf == diffFormat.Year)
{
return new int[] { year };
}
return new int[] { year, month };
#endregion
}
}
/// <summary>
/// 关于返回值形式的枚举
/// </summary>
public enum diffFormat
{
/// <summary>
/// 年数和月数
/// </summary>
YearMonth,
/// <summary>
/// 年数
/// </summary>
Year,
/// <summary>
/// 月数
/// </summary>
Month,
/// <summary>
/// 天数
/// </summary>
Day,
}
例:
DateTime dt1 = DateTime.Parse(dt.Rows[0]["mtime"].ToString());
DateTime dt2 = DateTime.Parse(dt.Rows[count - 1]["mtime"].ToString());
int[] kk = dateTimeDiff.toResult(dt1, dt2, diffFormat.Month);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)