您的位置:首页 > 其它

查找正序排列的List中缺失的日期数据的一个算法

2013-11-15 16:14 281 查看
Code:

public List<DateTime> getMissDateData()
{

DateTime[] keys = { DateTime.Now.AddDays(-5), DateTime.Now.AddDays(-3), DateTime.Now.AddDays(-1), };
List<DateTime> missingDate = new List<DateTime>();
for (int i = 0; i < keys.Length; i++)
{
logger.Info("Orginal data:"+keys[i]);
}

if (keys.Length > 0)
{
DateTime firstDate = keys[0];
if (firstDate.Day > 1)
{
int missingDateInt = firstDate.Day - 1;
for (int i = missingDateInt; i > 0; i--)
{
missingDate.Add(firstDate.AddDays(-i));
}
}
int keysLength = (keys[keys.Length - 1] - keys[0]).Days;

DateTime dtTemp = keys[0];
// firstDate = keys[0];
int count = keys.Length;
for (int i = 0; i < count; i++)
{
Console.WriteLine(dtTemp.ToString(keys[i].ToString("yyyy-MM-dd HH:mm:ss")));
while (DateTime.Compare(keys[i], dtTemp) != 0)
{
logger.InfoFormat("Actual Date:{0};  The date of the cumulative:{1}", keys[i].ToShortDateString(), dtTemp.ToShortDateString());
logger.InfoFormat("{0}", DateTime.Compare(keys[i], dtTemp));
//Console.WriteLine(String.Format("Actual Date:{0};  The date of the cumulative:{1}", keys[i].ToShortDateString(), dtTemp.ToShortDateString()));
// Console.WriteLine(String.Format("{0}", DateTime.Compare(keys[i], dtTemp)));
missingDate.Add(dtTemp);
dtTemp = dtTemp.AddDays(1);
}
dtTemp = dtTemp.AddDays(1);
}
}
else
{
logger.InfoFormat("Has no new data:{0}", keys.Length);
// Console.WriteLine(String.Format("Has no new data:{0}", keys.Length));
}

for (int i = 0; i < missingDate.Count; i++)
{
logger.Info("Missing data:" + missingDate[i].ToShortDateString());
//Console.WriteLine("Missing data:" + missingDate[i].ToShortDateString());
}
return missingDate;
}


欢迎大家吐槽。


.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐