zoukankan      html  css  js  c++  java
  • 八百年一次,这个月有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 
     
  • 相关阅读:
    goland 安装包激活码
    go目录文件
    php 单表数据转化为json格式
    thinkphp分页查询,后台处理怎么做
    用js 获取url 参数 页面跳转 ? 后的参数
    约瑟夫问题
    openstack配置
    Python数据结构
    Python常用排序算法
    scrapy使用
  • 原文地址:https://www.cnblogs.com/LoveJenny/p/2098836.html
Copyright © 2011-2022 走看看