问题描述
- C#求今天是一年中的第几周怎么算?
-
C#求今天是一年中的第几周怎么算?
解决方案
private int WeekOfYear(string date)
{
DateTime curDay = Convert.ToDateTime(date);
int firstdayofweek = Convert.ToInt32(Convert.ToDateTime(curDay.Year.ToString() + "- " + "1-1 ").DayOfWeek);
int days = curDay.DayOfYear;
int daysOutOneWeek = days - (7 - firstdayofweek);
if (daysOutOneWeek <= 0)
{
return 1;
}
else
{
int weeks = daysOutOneWeek / 7;
if (daysOutOneWeek % 7 != 0)
weeks ++;
return weeks + 1;
}
}
http://blog.csdn.net/letian_fly/article/details/2739618
解决方案二:
方法一:
private int WeekOfYear(string date)
{
DateTime curDay = Convert.ToDateTime(date);
int firstdayofweek = Convert.ToInt32(Convert.ToDateTime(curDay.Year.ToString() + "- " + "1-1 ").DayOfWeek);
int days = curDay.DayOfYear;
int daysOutOneWeek = days - (7 - firstdayofweek);
if (daysOutOneWeek <= 0)
{
return 1;
}
else
{
int weeks = daysOutOneWeek / 7;
if (daysOutOneWeek % 7 != 0)
weeks ++;
return weeks + 1;
}
}
方法二:
System.Globalization.GregorianCalendar gc = new System.Globalization.GregorianCalendar();
int weekOfYear = gc.GetWeekOfYear(DateTime.Now, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Monday);
解决方案三:
这个算法百度一下 网上太多了,不过大多都是C语言版本的,修改成C#应该挺简单的
解决方案四:
dt.DayOfYear/7