当前位置:网站首页>.net(C#)获取两个日期间隔的年月日
.net(C#)获取两个日期间隔的年月日
2022-08-04 23:34:00 【在路上走着走着】
年份:不满一年,算0年
月份:当前月,1号到月底算1月,2号到次月1号,3号到次月2号,依次类推,算1个月
天数:包含起止日期的天数
/// <summary>
///
/// </summary>
/// <param name="MinDate"></param>
/// <param name="MaxDate"></param>
/// <returns></returns>
private static GetDiffDateModel GetDiffDate(DateTime MinDate, DateTime MaxDate)
{
GetDiffDateModel result = new GetDiffDateModel();
//只比较年月日
MinDate = new DateTime(MinDate.Year, MinDate.Month, MinDate.Day);
MaxDate = new DateTime(MaxDate.Year, MaxDate.Month, MaxDate.Day);
if (MinDate >= MaxDate)
{
return result;
}
#region 年
#region 案例
/* #2022.01.01 2022.12.29 2022.12.31 0 #2022.01.01 2022.12.30 2022.12.31 0 #2022.01.01 2022.12.31 2022.12.31 1 #2022.01.01 2023.12.29 2023.12.31 1 #2022.01.01 2023.12.30 2023.12.31 1 #2022.01.01 2023.12.31 2023.12.31 2 #2022.01.02 2022.12.29 2023.01.01 0 #2022.01.02 2022.12.30 2023.01.01 0 #2022.01.02 2022.12.31 2023.01.01 0 #2022.01.03 2023.01.01 2023.01.02 0 #2022.01.03 2023.01.02 2023.01.02 1 #2022.01.03 2023.12.31 2023.01.02 1 #2022.01.03 2024.01.01 2024.01.02 2 */
#endregion
var _year = MaxDate.Year - MinDate.Year;
if (MinDate.Month == 1 && MinDate.Day == 1)
{
//如果是当前最后一天,年份+1
if (MaxDate == new DateTime(MaxDate.Year, 12, 31))
{
_year++;
}
}
else
{
if (_year == 0)//同一年
{
_year = 0;
}
else
{
if (MinDate.AddYears(_year).AddDays(-1) > MaxDate)
{
_year--;
}
}
}
result.Year = _year;
#endregion
#region 月
#region 案例
/* 2022年8月1号~2022年08月30号 2022年08月31号 算0月 2022年8月1号~2022年08月31号 2022年08月31号 算1月 2022年8月3号~2022年09月01号 2022年09月02号 算0月 2022年8月3号~2022年09月02号 2022年09月02号 算1月 2022年8月3号~2022年09月30号 2022年09月02号 算1月 2022年8月3号~2022年09月31号 2022年09月02号 算1月 2022年8月3号~2022年10月01号 2022年10月02号 算1月 2022年8月3号~2022年10月02号 2022年10月02号 算2月 */
#endregion
/* 逻辑:先把年的部分去掉,然后比较剩下的月份 */
var currMinDate = MinDate.AddYears(result.Year);
var _month = MaxDate.Month - currMinDate.Month;
if (currMinDate.Day == 1)
{
//如果是当月最后一天,月份+1
if (MaxDate.Day == new DateTime(MaxDate.Year, MaxDate.Month, 1).AddMonths(1).AddDays(-1).Day)
{
_month++;
}
}
else
{
if (_month == 0)//同一月
{
_month = 0;
}
else
{
if (currMinDate.AddMonths(_month).AddDays(-1) > MaxDate)
{
_month--;
}
}
}
result.Month = _month + (result.Year * 12);
#endregion
#region 日
result.Day = (MaxDate.Date - MinDate.Date).Days + 1;
#endregion
return result;
}
public class GetDiffDateModel
{
public int Year {
get; set; }
public int Month {
get; set; }
public int Day {
get; set; }
}
边栏推荐
- 「津津乐道播客」#397 厂长来了:怎样用科技给法律赋能?
- 一点点读懂Thremal(二)
- Service Mesh landing path
- Pytorch分布式训练/多卡/多GPU训练DDP的torch.distributed.launch和torchrun
- 仪表板展示 | DataEase看中国:数据呈现中国资本市场
- I was rejected by the leader for a salary increase, and my anger rose by 9.5K after switching jobs. This is my mental journey
- 资深游戏建模师告知新手,游戏场景建模师必备软件有哪些?
- 基于深度学习的路面坑洞检测(详细教程)
- 怎么将自己新文章自动推送给自己的粉丝(巨简单,学不会来打我)
- MySQL的安装与卸载
猜你喜欢
随机推荐
App测试和Web测试的区别
【手撕AHB-APB Bridge】~ AMBA总线 之 AHB
社区分享|腾讯海外游戏基于JumpServer构建游戏安全运营能力
对写作的一些感悟
对“为什么一些程序员很傲慢”的解读
加解密在线工具和进制转化在线工具
C语言实现扫雷 附带源代码
一点点读懂regulator(三)
uniapp sharing function - share to friends group chat circle of friends effect (sorting)
I was rejected by the leader for a salary increase, and my anger rose by 9.5K after switching jobs. This is my mental journey
kernel问题定位手段总结
web3.js
Day118.尚医通:订单列表、详情、支付
如何根据地址获取函数名
Shell expect 实战案例
[Cultivation of internal skills of string functions] strcpy + strcat + strcmp (1)
【云原生 · Kubernetes】Kubernetes运维
小黑leetcode之旅:95. 至少有 K 个重复字符的最长子串
KT6368A蓝牙的认证问题_FCC和BQB_CE_KC认证或者其它说明
NebulaGraph v3.2.0 Release Note, many optimizations such as the performance of querying the shortest path









