using
System;
using
System.Collections.Generic;
using
System.Globalization;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace
ConsoleApplication6
{
class
Program
{
static
void
Main(
string
[] args)
{
GregorianCalendar gc =
new
GregorianCalendar();
int
weekOfYear = gc.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
Console.WriteLine(
"当前第{0}周"
, weekOfYear);
DateTime startDate, lastDate;
for
(
int
i = 1; i <= 53; i++)
{
GetDaysOfWeeks(DateTime.Now.Year, i,
out
startDate,
out
lastDate);
Console.WriteLine(
"第{0}周"
, i);
Console.WriteLine(startDate);
Console.WriteLine(lastDate);
}
Console.ReadLine();
}
public
static
bool
GetDaysOfWeeks(
int
year,
int
index,
out
DateTime first,
out
DateTime last)
{
first = DateTime.MinValue;
last = DateTime.MinValue;
if
(year < 1700 || year > 9999)
{
return
false
;
}
if
(index < 1 || index > 53)
{
return
false
;
}
DateTime startDay =
new
DateTime(year, 1, 1);
DateTime endDay =
new
DateTime(year + 1, 1, 1).AddMilliseconds(-1);
int
dayOfWeek = 0;
if
(Convert.ToInt32(startDay.DayOfWeek.ToString(
"d"
)) > 0)
dayOfWeek = Convert.ToInt32(startDay.DayOfWeek.ToString(
"d"
));
if
(dayOfWeek == 0) { dayOfWeek = 7; }
if
(index == 1)
{
first = startDay.AddDays(7 - dayOfWeek - 6);
if
(dayOfWeek == 6)
{
last = first;
}
else
{
last = startDay.AddDays((7 - dayOfWeek));
}
}
else
{
first = startDay.AddDays((8 - dayOfWeek) + (index - 2) * 7);
last = first.AddDays(6);
发表评论