惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

云风的 BLOG
云风的 BLOG
Help Net Security
Help Net Security
Y
Y Combinator Blog
WordPress大学
WordPress大学
D
DataBreaches.Net
N
Netflix TechBlog - Medium
U
Unit 42
爱范儿
爱范儿
MyScale Blog
MyScale Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园 - 司徒正美
Google DeepMind News
Google DeepMind News
D
Docker
H
Help Net Security
Stack Overflow Blog
Stack Overflow Blog
宝玉的分享
宝玉的分享
博客园_首页
Microsoft Security Blog
Microsoft Security Blog
Engineering at Meta
Engineering at Meta
Know Your Adversary
Know Your Adversary
P
Privacy & Cybersecurity Law Blog
P
Proofpoint News Feed
T
Tenable Blog
S
Schneier on Security
V
Vulnerabilities – Threatpost
V
V2EX
T
Tor Project blog
Security Latest
Security Latest
S
Securelist
G
Google Developers Blog
NISL@THU
NISL@THU
Schneier on Security
Schneier on Security
Webroot Blog
Webroot Blog
小众软件
小众软件
Google Online Security Blog
Google Online Security Blog
阮一峰的网络日志
阮一峰的网络日志
W
WeLiveSecurity
IT之家
IT之家
I
InfoQ
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
月光博客
月光博客
I
Intezer
T
The Blog of Author Tim Ferriss
C
Cisco Blogs
博客园 - 【当耐特】
The GitHub Blog
The GitHub Blog
Cloudbric
Cloudbric
Scott Helme
Scott Helme
The Cloudflare Blog
L
LINUX DO - 热门话题

博客园 - 阿修

Entity FrameWork 单表对多实体 让Entity Framework启动不再效验__MigrationHistory表 Entity Framework搜索指定字段解决方案 Linux+Mono+WebService:CS1703: An assembly with the same identity--mscorlib DataTable.Rows.Remove(row) 与 DataTable.Rows[i].Delete()区别 [转摘]Lucene学习总结之一:全文检索的基本原理 Lucene.net根据Sort走到了不同的类处理 VPN Connections and Default Gateways - 阿修 牛!帮EXE等执行文件压缩的工具 进程通讯的多种方式 三星I9008删除程序清单 .NET编程中的部分效率问题 质量保证漫漫谈之QA、QC、QM的关系与区别 敏捷宣言 多维角度聊聊结对编程 在Windows下编译和.NET运行MemCached Serv-U服务器中文乱码问题的解决 请小心 管理学中四件不要做的事情
获取某年中第几个礼拜的第一天的函数
阿修 · 2012-03-05 · via 博客园 - 阿修

今天有朋友问题我如何获取某年中第几个礼拜的时间范围,似乎以前好像记得.NET有,不过今天一查没有翻到,在google搜索到这个,希望以后能自己用上,测试已经通过。

        public static void Main()
{

DateTime date1 = GoToWeek(2012, 2);
Console.Write(date1.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 culture
DayOfWeek 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 one
d = 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 on
int difference = (int)fdow - startDay;
// if it is positive (i.e. after first day of week), take away 7
if (difference > 0)
{
difference = difference - 7;
}

// already on week 1, so add desired number of weeks - 1 * days in week
if (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 calendar
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.CalendarWeekRule;
// get the first day of week for the current culture
DayOfWeek fdow = ci.DateTimeFormat.FirstDayOfWeek;
// return the week
return cal.GetWeekOfYear(date, cwr, fdow);
}