欢迎光临

木子屋

标题: C#计算指定日期是当年第几周

作者: dnawo

日期: 2013-05-16 03:41 PM

分类: Win编程

内容: C#自带类实现：
复制内容到剪贴板 程序代码//using System.Globalization;
public static int WeekOfYear(DateTime date)
{
CultureInfo culture = new CultureInfo("zh-CN");
culture.DateTimeFormat.CalendarWeekRule = CalendarWeekRule.FirstDay;
culture.DateTimeFormat.FirstDayOfWeek = DayOfWeek.Monday;
return culture.Calendar.GetWeekOfYear(date, culture.DateTimeFormat.CalendarWeekRule, culture.DateTimeFormat.FirstDayOfWeek);
}
自己写函数实现：
复制内容到剪贴板 程序代码public static int WeekOfYear(DateTime date)
{
//1.计算元旦是周几
DateTime FirstDay = DateTime.Parse(string.Format("{0}-{1}-{2}", date.Year, "1", "1"));
int dayofweek = 0;
switch (FirstDay.DayOfWeek)
{
case DayOfWeek.Monday:
dayofweek = 1;
break;
case DayOfWeek.Tuesday:
dayofweek = 2;
break;
case DayOfWeek.Wednesday:
dayofweek = 3;
break;
case DayOfWeek.Thursday:
dayofweek = 4;
break;
case DayOfWeek.Friday:
dayofweek = 5;
break;
case DayOfWeek.Saturday:
dayofweek = 6;
break;
case DayOfWeek.Sunday:
dayofweek = 7;
break;
}
//2.总天数补足第一周天数
int dayofyear = date.DayOfYear;
if (dayofweek > 1) dayofyear += (dayofweek - 1);
return (int)Math.Ceiling((decimal)dayofyear / 7);
}

+ 查看当前日志评论 (0)

登录

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 1.789 ms

返回

标题: C#计算指定日期是当年第几周

你没有权限发表评论

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 1.789 ms

返回

暂无评论

返回

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 1.836 ms

返回