当前位置: 代码迷 >> J# >> 依据年、月、周、求本月的起始日和结束日
  详细解决方案

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

热度:8915   发布时间:2013-02-25 00:00:00.0
根据年、月、周、求本月的起始日和结束日!
根据年、月、周、求本月的起始日和结束日!

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

根据年、月、周、求本月的起始日和结束日!
------解决方案--------------------------------------------------------
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 . . .
  相关解决方案