快捷搜索:  as  2018  FtCWSyGV  С˵  test  xxx  Ψһ  w3viyKQx

神秘大逃亡游戏下载:C获取某年中第几个礼拜的第一天



C#若何获取某年中第几个星期的光阴范围呢?.net没有现成的函数可以应用,于是自己写了一个。

public static void Mai神秘大逃亡游戏下载n()

{

DateTime date1 = GoToWeek(2012, 2);Console.Write(da神秘大逃亡游戏下载te1.ToString());

Console.Read();}

private static DateTime GoToWeek(int year, int week)

{// use the current culture

System.Globalization.CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture;

// get first day of week from cultureDayOfWeek fdow = ci.DateTimeFormat.FirstDayOfWeek;

// new empty Date (so starts 01/01/0001)

DateTime d = new DateTime();

// year starts at 1, so take away 1 from desired year to prevent going to the神秘大逃亡游戏下载 next oned = d.AddYears(year - 1);

// get day January 1st falls on

int startDay = (int)d.DayOfWeek;

// get the difference between the first day of the week, and the day January 1st starts onint difference = (int)fdow - startDay;

// if it is positive (i.e. after first day of week), take away 7if (difference > 0)

{difference = difference - 7;

}

// already on week 1, so add desired number of weeks - 1 * days in weekif (week > 1)

{d = d.AddDays((week - 1) * 7 + difference);

}

return d;}

private static int WeekOfYear(DateTime date)

{// use the current culture

System.Globalization.CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture;// use current culture's calenda神秘大逃亡游戏下载r

System.Globalization.Calendar cal = ci.Calendar;// get the calendar week rule (i.e. what determines the first week in the year)

System.Globalization.CalendarWeekRule cwr = ci.DateTimeFormat.CalendarWe神秘大逃亡游戏下载ekRule;// get the first day of week for the current culture

DayOfWeek fdow = ci.DateTimeFormat.FirstDayOfWeek;// return the week

return cal.GetWeekOfYear(date, cwr, fdow);}

您可能还会对下面的文章感兴趣: