统计数据时候读取年月进行分组统计,计算两个日期之间的总天数,计算当前时间的当月的开始时间和结束时间,计算某月的天数,计算某年的天数
/// <summary>
/// 填充年份的下拉列表
/// </summary>
/// <param name="year"></param>
public static void GetYear(DropDownList year)
{
int moy = 2014;
year.Items.Clear();
year.Items.Add(new ListItem("选择年份", ""));
for (int i = DateTime.Now.Year - moy; i >= 0; i--) year.Items.Add(new ListItem((moy + i).ToString() + "年", (moy + i).ToString()));
}
/// <summary>
/// 填充月份的下拉列表
/// </summary>
/// <param name="year"></param>
public static void GetMonth(DropDownList month)
{
month.Items.Clear();
month.Items.Add(new ListItem("选择月份", ""));
for (int i = 1; i <= 12; i++) month.Items.Add(new ListItem(i.ToString() + "月", i.ToString()));
//month.SelectedIndex = DateTime.Now.Month;
}
/// <summary>
/// 计算两个时间之间的总天数:如:1900-01-01与1900-01-01之间算一天
/// </summary>
/// <param name="dateStart">结束时间</param>
/// <param name="dateEnd">开始时间</param>
/// <returns></returns>
public static int DateDiff(DateTime dateEnd, DateTime dateStart)
{
DateTime start = Convert.ToDateTime(dateStart.ToShortDateString());
DateTime end = Convert.ToDateTime(dateEnd.ToShortDateString());
TimeSpan sp = end.Subtract(start);
return sp.Days + 1;
}
/// <summary>
/// 计算当前时间对应的月的天数,返回当月的开始时间和结束时间
/// </summary>
/// <param name="dtNow">月的结束时间的年月,不用日</param>
/// <param name="sTime">月的开始时间</param>
/// <returns></returns>
public static DateTime GetSETime(DateTime dtNow, ref DateTime sTime)
{
int days = DateTime.DaysInMonth(dtNow.Year, dtNow.Month);
sTime = Convert.ToDateTime(dtNow.Year.ToString("00") + "-" + dtNow.Month.ToString("00") + "-" + "01");
DateTime eTime = Convert.ToDateTime(dtNow.Year.ToString("00") + "-" + dtNow.Month.ToString("00") + "-" + days.ToString("00"));
return eTime;
}
/// <summary>
/// 计算某月的天数
/// </summary>
/// <param name="year">年</param>
/// <param name="month">月</param>
/// <returns></returns>
public static int GetYMDays(int year, int month)
{
int days = DateTime.DaysInMonth(year, month);
return days;
}
/// <summary>
/// 计算某年的天数
/// </summary>
/// <param name="year">年</param>
/// <returns></returns>
public static int GetYDays(int year)
{
int days = DateTime.IsLeapYear(year) ? 366 : 365;
return days;
}
评论列表: