StatisticQuery 类
private Dictionary<string, DateTime> GetTBHBDate(StatisticQuery model, string field)
{
Dictionary<string, DateTime> dic = new Dictionary<string, DateTime>();
DateTime TB = new DateTime();//同比开始日期 (年)
DateTime HB = new DateTime();//环比开始日期 (月)
DateTime fieldValue = new DateTime();
Type type = typeof(StatisticQuery);
// 根据字段名称获取 FieldInfo 对象
//FieldInfo fileinfo= type.GetField(field, BindingFlags.Instance | BindingFlags.NonPublic);
var fieldInfos = (System.Reflection.FieldInfo[])((System.Reflection.TypeInfo)type).DeclaredFields;
foreach (FieldInfo f in fieldInfos) {
if (f.Name.Contains(field) && !f.Name.Contains("1")) {
// 获取字段值
fieldValue = (DateTime)f.GetValue(model);
continue;
}
}
int daysInCurrentMonth = DateTime.DaysInMonth(fieldValue.Year, fieldValue.Month); // 获取查询日期当前月份的天数
bool isLastDayOfMonth = (daysInCurrentMonth == fieldValue.Day); // 判断查询日期是否为月末最后一天
if (!string.IsNullOrEmpty(model.setTime))//是否任意年度
{
//判断任意年度当月日期是否最后一天
int daysInRandomMonth = DateTime.DaysInMonth(Convert.ToDateTime(fieldValue.ToString("" + model.setTime + "-MM-01")).Year, Convert.ToDateTime(fieldValue.ToString("" + model.setTime + "-MM-01")).Month);
bool isLastDayOfMonthRandom = (daysInRandomMonth == Convert.ToDateTime(fieldValue.ToString("" + model.setTime + "-MM-01")).Day);
if (isLastDayOfMonth && !isLastDayOfMonthRandom)
{
//不是最后一天就改为最后一天
TB = Convert.ToDateTime(fieldValue.ToString("" + model.setTime + "-MM-01")).AddMonths(1).AddDays(-1);
}
else { TB = Convert.ToDateTime(fieldValue.ToString("" + model.setTime + "-MM-dd")); }
daysInRandomMonth = DateTime.DaysInMonth(Convert.ToDateTime(fieldValue.AddMonths(-1).ToString("" + model.setTime + "-MM-01")).Year, Convert.ToDateTime(fieldValue.AddMonths(-1).ToString("" + model.setTime + "-MM-01")).Month);
isLastDayOfMonthRandom = (daysInRandomMonth == Convert.ToDateTime(fieldValue.AddMonths(-1).ToString("" + model.setTime + "-MM-01")).Day);
if (isLastDayOfMonth && !isLastDayOfMonthRandom)
{
//不是最后一天就改为最后一天
HB = Convert.ToDateTime(fieldValue.AddMonths(-1).ToString("yyyy-MM-01")).AddMonths(1).AddDays(-1);
}
else { HB = Convert.ToDateTime(fieldValue.AddMonths(-1)); }
}
else
{
//上一年度
int daysInLastMonth = DateTime.DaysInMonth(fieldValue.AddYears(-1).Year, fieldValue.AddYears(-1).Month); // 获取当前月份的天数
bool isLastDayOfLastMonth = (daysInLastMonth == fieldValue.AddYears(-1).Day); // 判断是否为月末最后一天
if (isLastDayOfMonth && !isLastDayOfLastMonth)
{
//不是最后一天就改为最后一天
TB = Convert.ToDateTime(fieldValue.AddYears(-1).ToString("yyyy-MM-01")).AddMonths(1).AddDays(-1);
}
else
{
TB = Convert.ToDateTime(fieldValue.AddYears(-1));
}
daysInLastMonth = DateTime.DaysInMonth(fieldValue.AddMonths(-1).Year, fieldValue.AddMonths(-1).Month); // 获取当前月份的天数
isLastDayOfLastMonth = (daysInLastMonth == fieldValue.AddMonths(-1).Day); // 判断是否为月末最后一天
if (isLastDayOfMonth && !isLastDayOfLastMonth)
{
//不是最后一天就改为最后一天
HB = Convert.ToDateTime(fieldValue.AddMonths(-1).ToString("yyyy-MM-01")).AddMonths(1).AddDays(-1);
}
else
{
HB = Convert.ToDateTime(fieldValue.AddMonths(-1));
}
}
dic.Add("TB", TB);
dic.Add("HB", HB);
return dic;
}