当前位置:网站首页>.net (C#) get year month day between two dates
.net (C#) get year month day between two dates
2022-08-04 23:42:00 【walking on the road】
年份:不满一年,算0年
月份:当前月,1The number is calculated by the end of the month1月,2No. to next month1号,3No. to next month2号,依次类推,算1个月
天数:Number of days including start and end dates
/// <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)
{
//If it is the current last day,年份+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
/* 逻辑:Remove the year part first,Then compare the remaining months */
var currMinDate = MinDate.AddYears(result.Year);
var _month = MaxDate.Month - currMinDate.Month;
if (currMinDate.Day == 1)
{
//If it is the last day of the month,月份+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; }
}
边栏推荐
- 如何根据地址获取函数名
- Pytorch分布式训练/多卡/多GPU训练DDP的torch.distributed.launch和torchrun
- 应用联合、体系化推进。集团型化工企业数字化转型路径
- #yyds干货盘点#交换设备丢包严重的故障处理
- Implementing class target method exception using proxy object execution
- 吐槽 | 参加IT培训的正确姿势
- VMware NSX 4.0 -- 网络安全虚拟化平台
- 一点点读懂cpufreq(二)
- 【CVA估值训练营】财务建模指南——第一讲
- uniapp horizontal tab (horizontal scrolling navigation bar) effect demo (organization)
猜你喜欢
游戏3D建模入门,有哪些建模软件可以选择?
2022牛客暑期多校训练营5(BCDFGHK)
没有这些「伪需求」,产品经理的 KPI 怎么完成?
【七夕快乐篇】Nacos是如何实现服务注册功能的?
加解密在线工具和进制转化在线工具
统计单词(DAY 101)华中科技大学考研机试题
VMware NSX 4.0 -- 网络安全虚拟化平台
Implementation principle of golang coroutine
MySQL基础篇【子查询】
Bidding Announcement | Operation and Maintenance Project of Haina Baichuang Official Account
随机推荐
【CVA估值训练营】财务建模指南——第一讲
The Controller layer code is written like this, concise and elegant!
一点点读懂Thremal(二)
Develop a SpaceX website based on the Appian low-code platform
大师教你3D实时角色制作流程,游戏建模流程分享
Cython
kernel问题定位手段总结
安全软件 Avast 与赛门铁克诺顿 NortonLifeLock 合并案获英国批准,市值暴涨 43%
统计单词(DAY 101)华中科技大学考研机试题
一、爬虫基本概念
【无标题】
直接插入排序
The role of the annotation @ EnableAutoConfiguration and how to use
#yyds干货盘点#交换设备丢包严重的故障处理
什么是次世代建模(附学习资料)
407. 接雨水 II
Go 语言快速入门指南:什么是 TSL 安全传输层
uniapp 分享功能-分享给朋友群聊朋友圈效果(整理)
测试技术:关于上下文驱动测试的总结
DNS常见资源记录类型详解