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

推荐订阅源

Know Your Adversary
Know Your Adversary
WordPress大学
WordPress大学
Y
Y Combinator Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
阮一峰的网络日志
阮一峰的网络日志
G
Google Developers Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
F
Fortinet All Blogs
博客园 - 聂微东
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
J
Java Code Geeks
Vercel News
Vercel News
N
Netflix TechBlog - Medium
大猫的无限游戏
大猫的无限游戏
MyScale Blog
MyScale Blog
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
酷 壳 – CoolShell
酷 壳 – CoolShell
D
DataBreaches.Net
Hugging Face - Blog
Hugging Face - Blog
M
MIT News - Artificial intelligence
T
The Blog of Author Tim Ferriss
小众软件
小众软件
The GitHub Blog
The GitHub Blog
量子位
V
Visual Studio Blog
博客园_首页
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
C
CERT Recently Published Vulnerability Notes
The Cloudflare Blog
Spread Privacy
Spread Privacy
P
Proofpoint News Feed
T
Threat Research - Cisco Blogs
Simon Willison's Weblog
Simon Willison's Weblog
U
Unit 42
博客园 - 叶小钗
Apple Machine Learning Research
Apple Machine Learning Research
NISL@THU
NISL@THU
C
Cisco Blogs
T
Threatpost
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
Secure Thoughts
The Hacker News
The Hacker News
Attack and Defense Labs
Attack and Defense Labs
IT之家
IT之家
Help Net Security
Help Net Security
G
GRAHAM CLULEY
Jina AI
Jina AI

博客园 - 大尾巴狼

列相等与不等 (转载)一个项目经理的一些个人体会 为什么有时不能正确定位到异常的发生位置? 数据库对象搜索器 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),这只能这样了

结果: