-
public void getDays()
{
DateTime dt1 = new DateTime(2010, 01, 01);//初始化一個日期
DateTime dt2 = DateTime.Now;//取得今天日期
TimeSpan ts1 = dt1.Subtract(dt2);//TimeSpan得到dt1和dt2的時間間隔
int countday = ts1.Days;//取得兩個日期間的總天數
int weekday = 0;//工作日
//循環用來扣除總天數中的雙休日
for (int i = 0; i < countday; i++)
{
DateTime tempdt = dt1.Date.AddDays(i);
if (tempdt.DayOfWeek != System.DayOfWeek.Saturday && tempdt.DayOfWeek != System.DayOfWeek.Sunday)
{
weekday++;
}
}
Response.Write("總天數:" + countday.ToString());
Response.Write("工作日:" + weekday.ToString());