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

推荐订阅源

宝玉的分享
宝玉的分享
Apple Machine Learning Research
Apple Machine Learning Research
J
Java Code Geeks
V2EX - 技术
V2EX - 技术
Hacker News: Ask HN
Hacker News: Ask HN
T
Tailwind CSS Blog
V
Visual Studio Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
月光博客
月光博客
H
Hacker News: Front Page
D
DataBreaches.Net
GbyAI
GbyAI
Recorded Future
Recorded Future
IT之家
IT之家
H
Heimdal Security Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Schneier on Security
Schneier on Security
P
Privacy International News Feed
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Security Affairs
博客园 - 三生石上(FineUI控件)
M
MIT News - Artificial intelligence
Google Online Security Blog
Google Online Security Blog
L
LINUX DO - 最新话题
Google DeepMind News
Google DeepMind News
The Cloudflare Blog
L
LangChain Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
腾讯CDC
The Last Watchdog
The Last Watchdog
I
Intezer
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Hacker News - Newest:
Hacker News - Newest: "LLM"
Stack Overflow Blog
Stack Overflow Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
U
Unit 42
H
Help Net Security
Simon Willison's Weblog
Simon Willison's Weblog
Y
Y Combinator Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Schneier on Security
T
Tenable Blog
TaoSecurity Blog
TaoSecurity Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
小众软件
小众软件
B
Blog
S
Security @ Cisco Blogs
A
About on SuperTechFans
V
V2EX
T
The Exploit Database - CXSecurity.com

博客园 - 大尾巴狼

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

结果: