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

推荐订阅源

Hacker News: Ask HN
Hacker News: Ask HN
C
Cisco Blogs
The Hacker News
The Hacker News
T
Tor Project blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The GitHub Blog
The GitHub Blog
A
Arctic Wolf
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The Register - Security
The Register - Security
云风的 BLOG
云风的 BLOG
Simon Willison's Weblog
Simon Willison's Weblog
P
Palo Alto Networks Blog
Vercel News
Vercel News
C
CERT Recently Published Vulnerability Notes
I
InfoQ
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
M
MIT News - Artificial intelligence
I
Intezer
aimingoo的专栏
aimingoo的专栏
U
Unit 42
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LINUX DO - 热门话题
Microsoft Security Blog
Microsoft Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyberwarzone
Cyberwarzone
P
Proofpoint News Feed
P
Proofpoint News Feed
B
Blog
T
Threat Research - Cisco Blogs
博客园 - 叶小钗
Recorded Future
Recorded Future
Last Week in AI
Last Week in AI
N
News and Events Feed by Topic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Know Your Adversary
Know Your Adversary
Engineering at Meta
Engineering at Meta
G
Google Developers Blog
PCI Perspectives
PCI Perspectives
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
Application and Cybersecurity Blog
Application and Cybersecurity Blog
MyScale Blog
MyScale Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Schneier on Security
Schneier on Security
N
News | PayPal Newsroom
C
Cybersecurity and Infrastructure Security Agency CISA
H
Help Net Security
博客园 - 聂微东
H
Hackread – Cybersecurity News, Data Breaches, AI and More
G
GRAHAM CLULEY

博客园 - 大尾巴狼

列相等与不等 (转载)一个项目经理的一些个人体会 为什么有时不能正确定位到异常的发生位置? 数据库对象搜索器 XML遭遇缺省Namespace - 大尾巴狼 - 博客园 打开Outlook发邮件界面,发邮件 - 大尾巴狼 - 博客园 下载DataGrid内容,作为Excel可打开的文件 迁移用户的Documents and Settings 利用自定义属性,定义枚举值的详细文本,完美解决 - 大尾巴狼 - 博客园 利用自定义属性,定义枚举值的详细文本 多态相关的关键字的总结 住房商业贷款计算器 关于设置IIS目录的属性问题 只返回DB架构信息 用script让IE窗口最大化 - 大尾巴狼 - 博客园 可在多线程下TextBox中显示信息,和控制滚动的一个函数 DNN文件夹说明 脚本生成Guid - 大尾巴狼 - 博客园 DNN页面呈现过程
获得一个指定星期的起始和终止日期 - 大尾巴狼 - 博客园
大尾巴狼 · 2005-07-30 · via 博客园 - 大尾巴狼

在做一个项目的时候,
一、需要得到给定日期所属的星期,按星期统计。
二、然后得到这个星期的起始终止日期。
好像没有找到现成的方法,只能自己做一个。
觉得效率差了些,时间复杂度为O(365),不知道是否有更好的方法。

        [TestMethod]
        [Description(
"测试某星期的其实终止日期")]
        
public void testWeekDateScop()
        
{
            
//就测试这天了
            DateTime testDate = new DateTime(2005828);
            
            
//选择合适的日历
            System.Globalization.Calendar calendar = new System.Globalization.GregorianCalendar();

            
//把指定日期转换成年和所在星期
            int theYear = calendar.GetYear(testDate);
            
int theWeek = calendar.GetWeekOfYear(testDate, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Monday);

            
//得到指定星期的起始终止日期
            DateTime fromDate = DateTime.MinValue;
            DateTime toDate 
= DateTime.MinValue ;

            
//从一年的第一天到最后一天
            for ( int i = 0; i < calendar.GetDaysInYear(theYear); i++ )
            
{
                
//一年的第一天,到指定距离的日期
                DateTime thisDay = calendar.AddDays(new DateTime(theYear, 11), i);
                
//此处要和取得星期的策略一样(theWeek值)
                int curWeek = calendar.GetWeekOfYear(thisDay, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Monday);
                
if ( curWeek == theWeek )
                
{
                    
if ( fromDate == DateTime.MinValue ) fromDate = thisDay;
                    toDate 
= thisDay;
                }

            }


            TestContext.WriteLine(
"{0}年  第{1}周 星期:{4}  起始日期:{2}  终止日期:{3}", theYear, theWeek, fromDate, toDate, calendar.GetDayOfWeek(testDate).ToString());
        }

改进一下,复杂度变为,最好O(7),最差为(365),这只能这样了

结果: