EasyText, EasyLicense 的作者, https://github.com/EasyHelper Good Good Study,Day Day Up.

 

八百年一次,这个月有5个礼拜五,5个礼拜六,5个礼拜天

昨天在微博上看到有人说:

image

 

看看日历,的确是这样:

image

 

但是凭程序员的直觉,感觉下一次,应该不需要800年啊,于是做了如下测试:

 

判断有5个礼拜五,5个礼拜六,5个礼拜天的方法:

1:该月必须有31天

2:该月1号必须是星期5.

 

static void Main(string[] args)
{
    DateTime dtNow = DateTime.Now;

    while (dtNow < DateTime.MaxValue)
    {
        DateTime nextMonth = dtNow.AddMonths(1);
        DateTime firstDayOfNextMonth=new DateTime(nextMonth.Year,nextMonth.Month,1);
        if (DateTime.DaysInMonth(nextMonth.Year, nextMonth.Month) == 31 &&
            firstDayOfNextMonth.DayOfWeek == DayOfWeek.Friday)
        {
            Console.WriteLine("下一次是:{0}",firstDayOfNextMonth);
            break;
        }

        dtNow = nextMonth;
    }

    Console.ReadLine();
}
代码比较简单,无非就是一个月一个月的检查,并判断是否符合上面的两点。
运行结果如下:
image 
image 
 
 
那看下上次发生是什么时候吧,把DateTime nextMonth = dtNow.AddMonths(1); 
变成DateTime nextMonth = dtNow.AddMonths(-1);
接着把dtNow<DateTime.MaxValue 该为 DateTime.MinValue<=dtNow
 
就可以了,运行结果如下:
image 
image 
 
posted @   LoveJenny  阅读(28219)  评论(84编辑  收藏  举报
编辑推荐:
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
EasyText, EasyLicense 的作者, https://github.com/EasyHelper Good Good Study,Day Day Up.

 

点击右上角即可分享
微信分享提示