读书人

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

发布时间: 2012-12-25 16:18:28 作者: rapoo

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

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

根据年、月、周、求本月的起始日和结束日!
[解决办法]
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 . . .

读书人网 >.NET

热点推荐