计算按月分隔的两个日期之间的天数

时间:2011-06-17 07:58:19

标签: c# .net datetime

我需要计算两个日期(DateTime)之间的天数,但要扭曲。我想知道这两天的每个月都有多少天。有两种方式吗?

示例:

我的开始日期为30/03/2011,结束日期为05/04/2011,结果应该是:

var result = new Dictionary<DateTime, int>
             {
                { new DateTime(2011, 3, 1), 2 },
                { new DateTime(2011, 4, 1), 5 }
             };

7 个答案:

答案 0 :(得分:3)

您可以尝试这样的事情:

using System;
using System.Collections.Generic;

static class Program {

    // return dictionary tuple<year,month> -> number of days
    static Dictionary<Tuple<int, int>, int> GetNumberOfDays(DateTime start, DateTime end) {
        // assumes end > start
        Dictionary<Tuple<int, int>, int> ret = new Dictionary<Tuple<int, int>, int>();
        DateTime date = end;
        while (date > start) {
            if (date.Year == start.Year && date.Month == start.Month) {
                ret.Add(
                    Tuple.Create<int, int>(date.Year, date.Month),
                    (date - start).Days + 1);
                break;
            } else {
                ret.Add(
                    Tuple.Create<int, int>(date.Year, date.Month),
                    date.Day);
                date = new DateTime(date.Year, date.Month, 1).AddDays(-1);
            }
        }
        return ret;
    }

    static void Main(params string[] args) {
        var days = GetNumberOfDays(new DateTime(2011, 3, 1), new DateTime(2011, 4, 1));
        foreach (var m in days.Keys) {
            Console.WriteLine("{0}/{1} : {2} days", m.Item1, m.Item2, days[m]);
        }
    }

}

答案 1 :(得分:1)

您可以使用Time Period Library for .NET类:

// ----------------------------------------------------------------------
public Dictionary<DateTime,int> CountMonthDays( DateTime start, DateTime end )
{
  Dictionary<DateTime,int> monthDays = new Dictionary<DateTime, int>();

  Month startMonth = new Month( start );
  Month endMonth = new Month( end );

  if ( startMonth.Equals( endMonth ) )
  {
    monthDays.Add( startMonth.Start, end.Subtract( start ).Days );
    return monthDays;
  }

  Month month = startMonth;
  while ( month.Start < endMonth.End )
  {
    if ( month.Equals( startMonth ) )
    {
      monthDays.Add( month.Start, month.DaysInMonth - start.Day + 1 );
    }
    else if ( month.Equals( endMonth ) )
    {
      monthDays.Add( month.Start, end.Day );
    }
    else
    {
      monthDays.Add( month.Start, month.DaysInMonth );
    }
    month = month.GetNextMonth();
  }

  return monthDays;
} // CountMonthDays

用法:

// ----------------------------------------------------------------------
public void CountDaysByMonthSample()
{
  DateTime start = new DateTime( 2011, 3, 30 );
  DateTime end = new DateTime( 2011, 4, 5 );

  Dictionary<DateTime, int> monthDays = CountMonthDays( start, end );
  foreach ( KeyValuePair<DateTime, int> monthDay in monthDays )
  {
    Console.WriteLine( "month {0:d}, days {1}", monthDay.Key, monthDay.Value );
  }
  // > month 01.03.2011, days 2
  // > month 01.04.2011, days 5
} // CountDaysByMonthSample

答案 2 :(得分:1)

简单的是,快速否:

    DateTime StartDate = new DateTime(2011, 3, 30);
    DateTime EndDate = new DateTime(2011, 4, 5);

    int[] DaysPerMonth = new int[12];

    while (EndDate > StartDate)
    {
        DaysPerMonth[StartDate.Month]++;
        StartDate = StartDate.AddDays(1);
    }

答案 3 :(得分:1)

这是我的解决方案。我做了一个快速检查,它似乎工作...让我知道是否有任何问题:

    public Dictionary<DateTime, int> GetMontsBetween(DateTime startDate, DateTime EndDate)
    {
        Dictionary<DateTime, int> rtnValues = new Dictionary<DateTime, int>();
        DateTime startMonth = new DateTime(startDate.Year, startDate.Month, 1);
        DateTime endMonth = new DateTime(EndDate.Year, EndDate.Month, 1);
        //some checking
        if (startDate >= EndDate)
        {
            rtnValues.Add(startMonth, 0); // Or return null;
        }
        else if (startDate.Month == EndDate.Month && startDate.Year == EndDate.Year)
        {
            rtnValues.Add(startMonth, EndDate.Day - startDate.Day);
        }
        else
        {
            //Add first month remaining days
            rtnValues.Add(startMonth, DateTime.DaysInMonth(startDate.Year, startDate.Month) - startDate.Day);
            //Add All months days inbetween
            for (DateTime st = startMonth.AddMonths(1); st < endMonth; st = st.AddMonths(1))
            {
                rtnValues.Add(new DateTime(st.Year, st.Month, 1), DateTime.DaysInMonth(st.Year, st.Month) );
            }
            //Add last month days
            rtnValues.Add(new DateTime(EndDate.Year, EndDate.Month, 1), EndDate.Day);
        }
        return rtnValues;
    }

答案 4 :(得分:1)

使用内置的DateTime.DaysInMonth方法可以准确获取两个日期之间的总月数和天数的小例子,该方法可以使我们获得每月的天数,从而可以达到100%的准确性。

DateTime date1 = DateTime.Now.AddDays(60);
DateTime date2 = DateTime.Now;
TimeSpan ts = date1 - date2;
int totalDays = int.Parse(ts.TotalDays.ToString("0"));
int totalMonths = Math.Abs((date1.Month - date2.Month) + 12 * (date1.Year - date2.Year));
int months = 0;
int days = 0;
int totalDaysInMonths = 0;
for (int i = totalMonths; i > 0; i--)
{
    int month = date2.Month + i;
    int year = date1.Year;
    if (month > 12)
    {
        year++;
        int newMonth = month - 12;
        month = newMonth;
    }

    totalDaysInMonths = totalDaysInMonths + DateTime.DaysInMonth(year, month);
}

if (totalDays > totalDaysInMonths)
{
    months = totalMonths - 1;
    days = totalDays - totalDaysInMonths;
}

else if (totalDays < totalDaysInMonths)
{
    months = totalMonths - 1;
    int tempTotalDaysInMonths = 0;
    for (int i = months; i > 0; i--)
    {
        int month = date2.Month + i;
        int year = date1.Year;
        if (month > 12)
        {
            year++;
            int newMonth = month - 12;
            month = newMonth;
        }

        tempTotalDaysInMonths = tempTotalDaysInMonths + DateTime.DaysInMonth(year, month);
    }


    days = totalDays - tempTotalDaysInMonths;
}

else
{
    months = totalMonths;
}

return string.Format("{0} months and {1} days", months, days);

答案 5 :(得分:0)

使用linqpad进行非常快速和肮脏的运行:

DateTime start = DateTime.Parse("03/30/2011");
DateTime end = new DateTime(2011,04,05,23,59,59);

var startNextMonthFirstDay = new DateTime(start.Year, start.Month+1, 1);

var diffForStartMonth = (startNextMonthFirstDay - start);

var totalDiff = (end-start);

var diff = Math.Round(totalDiff.TotalDays);

var diffForEndMonth = diff - diffForStartMonth.Days;


Dictionary<DateTime, int> result = new Dictionary<DateTime, int>();
result.Add(new DateTime(start.Year, start.Month, 1), diffForStartMonth.Days);
result.Add(new DateTime(end.Year, end.Month, 1), (int)diffForEndMonth);

//Dictionary<DateTime,int>{{new DateTime(2011,3,1),2},{new DateTime(2011,4,1),5}}

result.Dump();

答案 6 :(得分:-2)

DateTime dt1 = new DateTime(2011, 12, 12);
DateTime dt2 = new DateTime(2011, 06, 12);

TimeSpan ts = dt1.Subtract(dt2);

String s = ts.Days.ToString();

MessageBox.Show(s);