欢迎光临

木子屋

标题: C#计算某一周第一天的日期

作者: dnawo

日期: 2013-05-29 04:50 PM

分类: Win编程

内容: 复制内容到剪贴板 程序代码using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(FirstDayOfWeek(2).ToShortDateString()); //2013/1/7
Console.ReadKey();
}
/// <summary>
/// 计算当年某一周第一天日期
/// </summary>
/// <param name="weeks"></param>
/// <returns></returns>
public static DateTime FirstDayOfWeek(int weeks)
{
return FirstDayOfWeek(DateTime.Now.Year, weeks);
}
/// <summary>
/// 计算某一年某一周第一天日期
/// </summary>
/// <param name="year"></param>
/// <param name="weeks"></param>
/// <returns></returns>
public static DateTime FirstDayOfWeek(int year, int weeks)
{
DateTime FirstDay = DateTime.Parse(string.Format("{0}-{1}-{2}", 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;
}
int dayofyear = (weeks - 1) * 7;
if (dayofweek > 1) dayofyear -= (dayofweek - 1);
if (dayofyear < 0) dayofyear = 0;
return FirstDay.AddDays(dayofyear);
}
}
}

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

登录

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

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 2.266 ms

返回

标题: C#计算某一周第一天的日期

你没有权限发表评论

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

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 2.266 ms

返回

暂无评论

返回

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

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 2.313 ms

返回