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

推荐订阅源

量子位
S
Secure Thoughts
S
Schneier on Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Cyberwarzone
Cyberwarzone
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Privacy International News Feed
L
Lohrmann on Cybersecurity
Schneier on Security
Schneier on Security
PCI Perspectives
PCI Perspectives
Google DeepMind News
Google DeepMind News
C
Cybersecurity and Infrastructure Security Agency CISA
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Latest news
Latest news
H
Hacker News: Front Page
月光博客
月光博客
Forbes - Security
Forbes - Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Security @ Cisco Blogs
WordPress大学
WordPress大学
Recent Commits to openclaw:main
Recent Commits to openclaw:main
aimingoo的专栏
aimingoo的专栏
宝玉的分享
宝玉的分享
D
Docker
U
Unit 42
Recorded Future
Recorded Future
Spread Privacy
Spread Privacy
Microsoft Security Blog
Microsoft Security Blog
Recent Announcements
Recent Announcements
云风的 BLOG
云风的 BLOG
Application and Cybersecurity Blog
Application and Cybersecurity Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
H
Heimdal Security Blog
Microsoft Azure Blog
Microsoft Azure Blog
V
Vulnerabilities – Threatpost
Vercel News
Vercel News
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
爱范儿
爱范儿
博客园 - 聂微东
Hugging Face - Blog
Hugging Face - Blog
L
LangChain Blog
G
GRAHAM CLULEY
Apple Machine Learning Research
Apple Machine Learning Research
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Blog — PlanetScale
Blog — PlanetScale
博客园 - 三生石上(FineUI控件)
罗磊的独立博客
Help Net Security
Help Net Security
Google Online Security Blog
Google Online Security Blog
S
SegmentFault 最新的问题

博客园 - answer

SQL查询重复记录 如何使用两台 NETGEAR 无线路由器进行无线中继(WDS) [转]写在UserControl销毁之时 给List排序( list sort) [转]检查本地DNS服务器是否正常工作及解决方法 SQLITE入门至精通 [转]WM手机,关于如何让手机一直运行下去,而不进入待机 [转]string表达式运算 [转]HTC G11 ROOT获取权限教程 [转]WebForm 与 winform 路径获取 [转]win7 系统装SQLServer2000 成功。 Mobile Development: Disable Windows Mobile 6.5 Start and Close Button [转]Windows Mobile: Hide StartButton in WinMo 6.5.x Windows CE 电源管理(转贴) [转]分享8个超棒的免费高质量图标搜索引擎 [转]Win7系统下VS2005_2008不识别WinCE5 SDK [转]windows 7 下ASP.net 本地配置 ( IIS 7) [转]SQLite存取二进制数据 [转]SelectObject() 装载字体 VC EVC
[转]取当前日期是在一年中的第几周
answer · 2012-12-05 · via 博客园 - answer

  今天在CSDN看到一个关于取当前日期是在一年中的第几周的帖子,上了头条推荐,里面解决方法的思路,是先取得该年第一天是星期几,然后取当前日期的DayOfYear再除以7,同时用DayOfYear对7取余,从而得到当前日期是一年中的第几周。

    这种方法不但麻烦,而且效率低,其实C#里有GregorianCalendar这样一个类,只需要两行代码,就可以很轻松的完成这个任务的,只是很多人不知道,或是遗忘了。

using System.Globalization;

GregorianCalendar gc 

= new GregorianCalendar();
int weekOfYear = gc.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstDay, DayOfWeek.Monday);

   
    写成通用的方法,获取某一日期是该年中的第几周

using System.Globalization;/// <summary>
/// 获取某一日期是该年中的第几周
/// </summary>
/// <param name="dt">日期</param>
/// <returns>该日期在该年中的周数</returns>
private int GetWeekOfYear(DateTime dt)
{
    GregorianCalendar gc 
= new GregorianCalendar();
    
return gc.GetWeekOfYear(dt, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
}


    以前还在CSDN上遇到这样一个问
题,就是计算某一年有多少周,同样可以用这个类的方法来解决

using System.Globalization;/// <summary>
/// 获取某一年有多少周
/// </summary>
/// <param name="year">年份</param>
/// <returns>该年周数</returns>
private int GetWeekAmount(int year)
{
    DateTime end 
= new DateTime(year, 1231);  //该年最后一天
    System.Globalization.GregorianCalendar gc = new GregorianCalendar();
    
return gc.GetWeekOfYear(end, CalendarWeekRule.FirstDay, DayOfWeek.Monday);  //该年星期数
}

来自: http://www.cnblogs.com/supers/archive/2008/11/17/1335182.html