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

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
U
Unit 42
Google DeepMind News
Google DeepMind News
博客园 - 司徒正美
Y
Y Combinator Blog
F
Fortinet All Blogs
云风的 BLOG
云风的 BLOG
T
Tailwind CSS Blog
G
Google Developers Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
罗磊的独立博客
D
DataBreaches.Net
T
The Blog of Author Tim Ferriss
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
MyScale Blog
MyScale Blog
N
Netflix TechBlog - Medium
Microsoft Security Blog
Microsoft Security Blog
GbyAI
GbyAI
P
Proofpoint News Feed
Jina AI
Jina AI
B
Blog RSS Feed
腾讯CDC
阮一峰的网络日志
阮一峰的网络日志
D
Docker

博客园 - 阿修

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);
}