当前位置:网站首页>.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; }
}
边栏推荐
- Community Sharing|Tencent Overseas Games builds game security operation capabilities based on JumpServer
- Basic web in PLSQL
- 堪称奔驰“理财产品”,空间媲美宝马X5,采用了非常运动的外观
- 956. 最高的广告牌
- [Cultivation of internal skills of string functions] strcpy + strcat + strcmp (1)
- 三、实战---爬取百度指定词条所对应的结果页面(一个简单的页面采集器)
- MongoDB权限验证开启与mongoose数据库配置
- MySQL增删改查基础
- The role of @Async annotation and how to implement asynchronous listening mechanism
- 社区分享|腾讯海外游戏基于JumpServer构建游戏安全运营能力
猜你喜欢

基于深度学习的路面坑洞检测(详细教程)

深度|医疗行业勒索病毒防治解决方案

怎么将自己新文章自动推送给自己的粉丝(巨简单,学不会来打我)

Ab3d.PowerToys and Ab3d.DXEngine Crack

MySQL基础篇【聚合函数】

PID Controller Improvement Notes No. 7: Improve the anti-overshoot setting of the PID controller

使用OpenCV实现一个文档自动扫描仪

社区分享|腾讯海外游戏基于JumpServer构建游戏安全运营能力

407. 接雨水 II

Ab3d.PowerToys and Ab3d.DXEngine Crack
随机推荐
没有这些「伪需求」,产品经理的 KPI 怎么完成?
请你说一下final关键字以及static关键字
2022年华数杯数学建模
golang 协程的实现原理
Ab3d.PowerToys and Ab3d.DXEngine Crack
三、实战---爬取百度指定词条所对应的结果页面(一个简单的页面采集器)
小黑leetcode之旅:95. 至少有 K 个重复字符的最长子串
The role of @Async annotation and how to implement asynchronous listening mechanism
npm基本操作及命令详解
一点点读懂Thremal(二)
Statistical words (DAY 101) Huazhong University of Science and Technology postgraduate examination questions
App测试和Web测试的区别
话题 | 雾计算和边缘计算有什么区别?
MySQL的安装与卸载
美团二面:Redis与MySQL双写一致性如何保证?
[QNX Hypervisor 2.2用户手册]10.6 vdev mc146818
jenkins发送邮件系统配置
typeScript-promise
The role of @ Import annotations as well as how to use
d枚举生成位