首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

根据年、月、周、求本月的起始日和结束日!解决思路

2012-02-19 
根据年、月、周、求本月的起始日和结束日!根据年、月、周、求本月的起始日和结束日!根据年、月、周、求本月的起始日

根据年、月、周、求本月的起始日和结束日!
根据年、月、周、求本月的起始日和结束日!

根据年、月、周、求本月的起始日和结束日!

根据年、月、周、求本月的起始日和结束日!

[解决办法]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(GetStartDate(2011, 8, 2));
Console.WriteLine(GetEndDate(2011, 8, 2));
}

static DateTime GetStartDate(int year, int month, int week)
{
if (week < 1) return default(DateTime);
if (week == 1)
return new DateTime(year, month, 1);
else
return GetEndDate(year, month, week - 1).AddDays(1);
}

static DateTime GetEndDate(int year, int month, int week)
{
if (week < 1) return default(DateTime);
DateTime dt = new DateTime(year, month, 1);
if (week == 1) 
return new DateTime(year, month, 1).AddDays(6 - (int)(new DateTime(year, month, 1)).DayOfWeek);
else
return GetEndDate(year, month, week - 1).AddDays(7);
}
}
}
2011-8-7 0:00:00
2011-8-13 0:00:00
Press any key to continue . . .

热点排行