1. 获取某段日期范围内的所有日期
/// <summary>
/// 获取某段日期范围内的所有日期,以数组形式返回
/// </summary>
/// <param name="dt1">开始日期</param>
/// <param name="dt2">结束日期</param>
/// <returns></returns>
private DateTime[] GetAllDays(DateTime dt1, DateTime dt2)
{
List<DateTime> listDays = new List<DateTime>();
DateTime dtDay = new DateTime();
for (dtDay = dt1; dtDay.CompareTo(dt2) <= 0; dtDay = dtDay.AddDays(1))
{
listDays.Add(dtDay);
}
return listDays.ToArray();
}
2. 根据年份和周次获得对应的时间范围
/// <summary>
/// 根据年份和周次获得对应的时间范围
/// </summary>
/// <param name="yearInt"></param>
/// <param name="weekInt"></param>
/// <returns></returns>
private string range(int yearInt, int weekInt)
{
int year, year1, year2, month1, month2, day1, day2, week, sum = 0;
int[] days = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int i;
year = yearInt;
week = weekInt;
sum += 7 * (week - 1); //修正总天数
switch (DateTime.Parse(year + "-1-1").DayOfWeek)
{
case DayOfWeek.Tuesday: { sum -= 1; break; }
case DayOfWeek.Wednesday: { sum -= 2; break; }
case DayOfWeek.Thursday: { sum -= 3; break; }
case DayOfWeek.Friday: { sum -= 4; break; }
case DayOfWeek.Saturday: { sum -= 5; break; }
case DayOfWeek.Sunday: { sum -= 6; break; }
default:
break;
}
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
{ days[1] = 29; } for (i = 0; i < days.Length; i++)
{
if (sum >= days[i]) { sum -= days[i]; } else { break; }
} year1 = year; month1 = i + 1; day1 = sum + 1;//修改开始日期
if (day1 < 1)
{ day1 += days[11]; month1 = 12; year1--; }
year2 = year1; month2 = month1; day2 = day1 + 6;
if (day2 > days[month1 - 1])
{
day2 -= days[month1 - 1];
month2++; if (month2 > 12) { month2 = 1; year2++; }
}
string ss = year1 + "年" + month1 + "月" + day1 + "日~" + year2 + "年" + month2 + "月" + day2 + "日";
return ss;
}
3. 根据系统当前时间获得今天是今年的第几周
/// <summary>
/// 根据系统当前时间获得今天是今年的第几周
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
private int WeekOfYear(string date)
{
DateTime curDay = Convert.ToDateTime(date);
int firstdayofweek = Convert.ToInt32(Convert.ToDateTime(curDay.Year.ToString() + "- " + "1-1 ").DayOfWeek);
int days = curDay.DayOfYear;
int daysOutOneWeek = days - (7 - firstdayofweek);
if (daysOutOneWeek <= 0)
{
return 1;
}
else
{
int weeks = daysOutOneWeek / 7;
if (daysOutOneWeek % 7 != 0)
weeks++;
return weeks + 1;
}
}
4.datetime.now.tostring("dddd");获得当前日期的星期几。
datetime各种处理。记录下,备不时之需。,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/fanshaomin/p/3817167.html