Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 两个日期之间的月差_C#_Datetime - Fatal编程技术网

C# 两个日期之间的月差

C# 两个日期之间的月差,c#,datetime,C#,Datetime,我试图计算一个日期列和今天之间的月差。我们在csharp中有没有像monthdiff或datediff这样的方法来实现这个功能?我的代码的问题是,若提交日期和年份不同,那个么它就会中断 bool isDateAccepted = ((SubmissionDate.Month - DateTime.Now.Month) < 6) bool isDateAccepted=((SubmissionDate.Month-DateTime.Now.Month),因此您必须定义平均天数,以检查月份数

我试图计算一个日期列和今天之间的月差。我们在csharp中有没有像monthdiff或datediff这样的方法来实现这个功能?我的代码的问题是,若提交日期和年份不同,那个么它就会中断

bool isDateAccepted = ((SubmissionDate.Month - DateTime.Now.Month) < 6)
bool isDateAccepted=((SubmissionDate.Month-DateTime.Now.Month)<6)

不要直接比较
月份
变量,因为正如您所注意到的,当月份数字“结束”时,它将中断

相反,减去
DateTime
对象以获得
TimeSpan
,然后使用该
TotalDays
属性:

bool isDateAccepted = ((SubmissionDate - DateTime.Now).TotalDays < 6 * 30)
bool isDateAccepted=((SubmissionDate-DateTime.Now)。总天数<6*30)

<代码> TimeSpA不考虑<代码>月> /COD>,因此您必须定义平均天数,以检查月份数。

< P>您可以计算总月份并减去它们:

public int MonthDifference(Date a, Date b)
{
   int totalMonthsA = a.Year*12 + a.Month;
   int totalMonthsB = b.Year*12 + b.Month;
   return totalMonthsA - totalMonthsB;
}

您始终可以在提交日期后添加6个月,并将其与当前日期进行比较

bool isDateAccepted = (submissionDate.AddMonths(6) > DateTime.Now);

这是一个太晚的答案,当我检查时,没有人回答关于月份的确切分数,就像我们在
时间跨度中所做的那样。例如,TotalDays

例如,
20-01-2021
20-03-2021
之间的总月数结果是什么? 我想应该是
2.0xx
,但是如果你把2月看作30天,你会得到一个不同的答案“
1.8xx
或者别的什么”

因此,我根据每个月选定日期之间的天数进行了计算

  • 首先,我得到了所选两个日期之间的天数列表,如中所示
  • 然后我按月份对它们进行分组并进行计算
守则:

public static double GetTotalMonths(this DateTimeOffset firstDate, DateTimeOffset thru)
{
    var days = firstDate.EachDayTill(thru).ToList();
    return days.GroupBy(d => new {d.Month, d.Year,})
        .Sum(g => (g.Count() * 1.00) / (DateTime.DaysInMonth(g.Key.Year, g.Key.Month) * 1.00));
}


public static IEnumerable<DateTimeOffset> EachDayTill(this DateTimeOffset from, DateTimeOffset thru)
{
    for (var day = new DateTimeOffset(from.Date, TimeSpan.Zero); day.Date <= thru.Date; day = day.AddDays(1))
        yield return day;
}
publicstaticdouble-GetTotalMonths(此DateTimeOffset-firstDate,DateTimeOffset-thru)
{
var days=firstDate.eachDaytil(thru.ToList();
返回天数.GroupBy(d=>new{d.Month,d.Year,})
.Sum(g=>(g.Count()*1.00)/(DateTime.DaysInMonth(g.Key.Year,g.Key.Month)*1.00));
}
公共静态IEnumerable EachDayTill(此DateTimeOffset from,DateTimeOffset thru)
{

for(var day=新的DateTimeOffset(from.Date,TimeSpan.Zero);day.Date下层选民是否愿意发表评论?我认为这是一个有效的方法。我这样做是因为它有缺陷。以2014年3月1日为例,你的方法会告诉系统从2014年3月1日到2014年8月28日的日期在范围内。但人们会认为2014年8月29日到31日也在6个月内。你可以使用365/2f,但也会有类似的错误。@wes很公平,我同意使用平均值并不是一个“理想”的解决方案(当然,这主要是因为我们的月系统同样存在缺陷)。@weston顺便说一句,我非常喜欢你的解决方案;认为它非常聪明。如果OP想考虑月中的某一天,这是一个非常优雅的解决方案。