62,243
社区成员




DateTime begin_dt = DateTime.Parse("2012-12-28");
DateTime end_dt = DateTime.Parse("2013-12-28");
Dictionary<string,int> list=new Dictionary<string,int> ();
string _preMonth=begin_dt.ToString("yyyy-MM");
int count=0;
while(begin_dt<=end_dt)
{
string _currentMonth=begin_dt.ToString("yyyy-MM");
if(_preMonth==_currentMonth)
{
count+=1;
string _endMonth = end_dt.ToString("yyyy-MM");
if (_preMonth == _endMonth && begin_dt == end_dt)
{
list.Add(_preMonth, count);
}
}
else
{
list.Add(_preMonth,count);
count=1;
_preMonth=begin_dt.ToString("yyyy-MM");
}
begin_dt = begin_dt.AddDays(1);
}
/* list
+ [0] {[2012-12, 4]} System.Collections.Generic.KeyValuePair<string,int>
+ [1] {[2013-01, 31]} System.Collections.Generic.KeyValuePair<string,int>
+ [2] {[2013-02, 28]} System.Collections.Generic.KeyValuePair<string,int>
+ [3] {[2013-03, 31]} System.Collections.Generic.KeyValuePair<string,int>
+ [4] {[2013-04, 30]} System.Collections.Generic.KeyValuePair<string,int>
+ [5] {[2013-05, 31]} System.Collections.Generic.KeyValuePair<string,int>
+ [6] {[2013-06, 30]} System.Collections.Generic.KeyValuePair<string,int>
+ [7] {[2013-07, 31]} System.Collections.Generic.KeyValuePair<string,int>
+ [8] {[2013-08, 31]} System.Collections.Generic.KeyValuePair<string,int>
+ [9] {[2013-09, 30]} System.Collections.Generic.KeyValuePair<string,int>
+ [10] {[2013-10, 31]} System.Collections.Generic.KeyValuePair<string,int>
+ [11] {[2013-11, 30]} System.Collections.Generic.KeyValuePair<string,int>
+ [12] {[2013-12, 28]} System.Collections.Generic.KeyValuePair<string,int>
*/
private string DateDiff(DateTime DateTime1, DateTime DateTime2)
{string dateDiff = null;
TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
TimeSpan ts = ts1.Subtract(ts2).Duration();
dateDiff = ts.Days.ToString()+"天"+ ts.Hours.ToString()+"小时"+ ts.Minutes.ToString()+"分钟"+ ts.Seconds.ToString()+"秒";
return dateDiff;
}