主要计算内容:
1、取得日期是当年的第几天
2、取得指定日期是星期几
3、根据每月一号是星期几以及取得的时第几周的星期几来计算符合上面条件的本月日期。
演示如下:
显示的时从2011年9月7日开始,显示7个月的,显示的是第一个周二 的datetime
纯使用公式计算,后面还有一种方法使用了datetime自带的方法一样解决问题。
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// handle of Button_ShowDate click
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button_ShowDate_Click(object sender, EventArgs e)
{
DateTime beginDate = Convert.ToDateTime(TextBox_BeginDate.Text.Trim());
int month_Num = Convert.ToInt32(TextBox_monthNum.Text.Trim());//the quntity of month you want to show
int weekNo = Convert.ToInt32(TextBox_No.Text.Trim());// the week No. from 1 to 4
int weekDay = Convert.ToInt32(TextBox_weekDay.Text.Trim());//the day of week from 1 to 7
int deTimeHour = Convert.ToInt32(TextBox_timeHour.Text.Trim());// the end hour
int deTimeMinute = Convert.ToInt32(TextBox_timeMinute.Text.Trim());//the end minute
//get the list of dateTime
List<DateTime> datetimeList = GetDateList(beginDate, month_Num, weekNo, weekDay, deTimeHour, deTimeMinute);
String StrDateList = null;
for (int i = 0; i < datetimeList.Count; i++)
{
StrDateList += datetimeList[i].ToString("yyyy-MM-dd HH:mm:ss") + " ";
}
//show datetime
Label_showDate.Text = StrDateList;
}
/// <summary>
/// judge if it is a Bissextile 判断是否是闰年
/// </summary>
/// <param name="year"></param>
/// <returns></returns>
public bool IsBissextile(int year)
{
return ((year % 4 == 0 && year % 100!=0) || (year % 400 == 0));
}
/// <summary>
/// get the day number of the date from this year begin 取得日期是当年的第几天
/// </summary>
/// <param name="year"></param>
/// <param name="month"></param>
/// <param name="day"></param>
/// <returns></returns>
public int GetDayNum(int year ,int month ,int day)
{
int dayNum = day;
if (month > 1)
{
for (int i=1; i < month; i++)
{
switch (i)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
dayNum += 31;
break;
case 4:
case 6:
case 9:
case 11:
dayNum += 30;
break;
case 2:
dayNum += IsBissextile(year) ? 29 : 28;
break;
}
}
}
return dayNum;
}
/// <summary>
/// get the dateList
/// </summary>
/// <param name="beginDatetime">begin dateTime</param>
/// <param name="month_Num">the quntity of month you want to show </param>
/// <param name="weekNo">he week No. from 1 to 4</param>
/// <param name="weekDay">the day of week from 1 to 7</param>
/// <param name="deHour">the end hour</param>
/// <param name="deMinute">the end Minute</param>
/// <returns></returns>
///
public List<DateTime> GetDateList(DateTime beginDatetime, int month_Num, int weekNo,int weekDay,int deHour,int deMinute)
{
int year = Convert.ToInt32(beginDatetime.ToString("yyyy"));//the begin year
int month = Convert.ToInt32(beginDatetime.Month);//the begin month
int day = Convert.ToInt32(beginDatetime.Day);//the begin day
int minute = Convert.ToInt32(beginDatetime.Minute);//the begin minute
int hour = Convert.ToInt32(beginDatetime.Hour);//the begin hour
int dayNumOfBeginMonth = GetDayNum( year , month , 1);//the begin day of a month
int weekDayOfBeginMonth = Convert.ToInt32(GetWeekDay(year, dayNumOfBeginMonth));//the day of week of the begin day of a month
int dayOfMonth = 0;//the day of the month that meet the requirments
List<DateTime> dateTimeList = new List<DateTime>();//dateList to return
DateTime datetime ;//datetime to store the datetime temporarily
int yearForlist = 0;//store the year temporarily
int monthForList = 0;//store the month temporarily
int dayForList = 0;//store the day temporarily
int beginDayNum = 0;//store the begin day number of the month temporarily
int beginweekDay = 0;//store the begin day 's day of week temporarily
dayOfMonth = GetDay(weekDayOfBeginMonth, weekDay, weekNo);
//not Overdue
if (day < dayOfMonth || (day == dayOfMonth && hour < deHour) || (day == dayOfMonth && hour == deHour && minute < deMinute))
{
datetime = new DateTime(year,month,dayOfMonth,deHour,deMinute,0);
dateTimeList.Add(datetime);
for (int i = 1; i < month_Num;i++ )
{
yearForlist = year + (month + i - 1) / 12;
monthForList =(month+i)%12==0?12:(month+i)%12;
beginDayNum = GetDayNum(yearForlist, monthForList, 1);
beginweekDay = Convert.ToInt32(GetWeekDay(yearForlist, beginDayNum));
dayForList = GetDay(beginweekDay, weekDay, weekNo);
datetime = new DateTime(yearForlist, monthForList, dayForList, deHour,deMinute,0);
dateTimeList.Add(datetime);
}
}
else//Overdue
{
for (int i = 1; i < month_Num+1; i++)
{
yearForlist = year + (month + i - 1) / 12;
monthForList = (month + i) % 12 == 0 ? 12 : (month + i) % 12;
beginDayNum = GetDayNum(yearForlist, monthForList, 1);
beginweekDay = Convert.ToInt32(GetWeekDay(yearForlist, beginDayNum));
dayForList = GetDay(beginweekDay, weekDay, weekNo);
datetime = new DateTime(yearForlist, monthForList, dayForList, deHour, deMinute, 0);
dateTimeList.Add(datetime);
}
}
return dateTimeList;
}
/// <summary>
/// get the date' day of week 取得指定日期是星期几
/// </summary>
/// <param name="year"></param>
/// <param name="dayNum"></param>
/// <returns></returns>
public double GetWeekDay(int year,int dayNum)
{
double weekDay = (Math.Truncate((double)year-1)+Math.Truncate((((double)year-1)/4))-Math.Truncate(((double)year-1)/100)+Math.Truncate(((double)year-1)/400)+dayNum)%7;
weekDay = (weekDay<0)?(weekDay+7):weekDay;
return (weekDay);
}
/// <summary>
/// get the day of the month 根据每月一号是星期几以及取得的时第几周的星期几来计算符合上面条件的本月日期
/// </summary>
/// <param name="beginweekDay"></param>
/// <param name="weekDay"></param>
/// <param name="weekNo"></param>
/// <returns></returns>
public int GetDay(int beginweekDay,int weekDay,int weekNo)
{
int day = 0;
if (beginweekDay <= weekDay)
{
day = 7 * weekNo - beginweekDay + weekDay - 6;
}
else
{
day = 7 * weekNo - beginweekDay + weekDay + 1;
}
return day;
}
}
经过查看datetime的方法发现上面有两个方法可以直接省去,使用 Convert.ToInt32((new DateTime(yearForlist, monthForList, 1)).DayOfWeek);可以直接去到指定日期是星期几,所以代码修改如下:
/ <summary>
// /// get the dateList
/ </summary>
/ <param name="beginDatetime">begin dateTime</param>
/ <param name="month_Num">the quntity of month you want to show </param>
/ <param name="weekNo">he week No. from 1 to 4</param>
/ <param name="weekDay">the day of week from 1 to 7</param>
/ <param name="deHour">the end hour</param>
/ <param name="deMinute">the end Minute</param>
/ <returns></returns>
/
public List<DateTime> GetDateList(DateTime beginDatetime, int month_Num, int weekNo, int weekDay, int deHour, int deMinute)
{
int year = Convert.ToInt32(beginDatetime.ToString("yyyy"));//the begin year
int month = Convert.ToInt32(beginDatetime.Month);//the begin month
int day = Convert.ToInt32(beginDatetime.Day);//the begin day
int minute = Convert.ToInt32(beginDatetime.Minute);//the begin minute
int hour = Convert.ToInt32(beginDatetime.Hour);//the begin hour
int weekDayOfBeginMonth = Convert.ToInt32((new DateTime(year, month, 1)).DayOfWeek);
weekDayOfBeginMonth = weekDayOfBeginMonth == 0 ? 7 : weekDayOfBeginMonth;
int dayOfMonth = 0;//the day of the month that meet the requirments
List<DateTime> dateTimeList = new List<DateTime>();//dateList to return
DateTime datetime;//datetime to store the datetime temporarily
int yearForlist = 0;//store the year temporarily
int monthForList = 0;//store the month temporarily
int dayForList = 0;//store the day temporarily
int beginweekDay = 0;//store the begin day 's day of week temporarily
dayOfMonth = GetDay(weekDayOfBeginMonth, weekDay, weekNo);
//not Overdue
if (day < dayOfMonth || (day == dayOfMonth && hour < deHour) || (day == dayOfMonth && hour == deHour && minute < deMinute))
{
datetime = new DateTime(year, month, dayOfMonth, deHour, deMinute, 0);
dateTimeList.Add(datetime);
for (int i = 1; i < month_Num; i++)
{
yearForlist = year + (month + i - 1) / 12;
monthForList = (month + i) % 12 == 0 ? 12 : (month + i) % 12;
beginweekDay = Convert.ToInt32((new DateTime(yearForlist, monthForList, 1)).DayOfWeek);
beginweekDay = beginweekDay == 0 ? 7 : beginweekDay;
dayForList = GetDay(beginweekDay, weekDay, weekNo);
datetime = new DateTime(yearForlist, monthForList, dayForList, deHour, deMinute, 0);
dateTimeList.Add(datetime);
}
}
else//Overdue
{
for (int i = 1; i < month_Num + 1; i++)
{
yearForlist = year + (month + i - 1) / 12;
monthForList = (month + i) % 12 == 0 ? 12 : (month + i) % 12;
beginweekDay = Convert.ToInt32((new DateTime(yearForlist, monthForList, 1)).DayOfWeek);
beginweekDay = beginweekDay == 0 ? 7 : beginweekDay;
dayForList = GetDay(beginweekDay, weekDay, weekNo);
datetime = new DateTime(yearForlist, monthForList, dayForList, deHour, deMinute, 0);
dateTimeList.Add(datetime);
}
}
return dateTimeList;
}
// /// <summary>
// /// get the day of the month
// /// </summary>
// /// <param name="beginweekDay"></param>
// /// <param name="weekDay"></param>
// /// <param name="weekNo"></param>
// /// <returns></returns>
public int GetDay(int beginweekDay, int weekDay, int weekNo)
{
int day = 0;
if (beginweekDay <= weekDay)
{
day = 7 * weekNo - beginweekDay + weekDay - 6;
}
else
{
day = 7 * weekNo - beginweekDay + weekDay + 1;
}
return day;
}
只是用上面两个函数就可以完成原来的功能。
参考文献: