微软农历返回闰年闰月值使用不是很理解,发现很多问题,添加不少逻辑,查2000-2022暂时未发现问题。

        static string 月历加载(int 查年, int 查月, int 查日)
        {
            System.Globalization.ChineseLunisolarCalendar 微软农历 = new System.Globalization.ChineseLunisolarCalendar();
            int 天 = 微软农历.GetDayOfMonth(new DateTime(查年, 查月, 查日)),
            月 = 微软农历.GetMonth(new DateTime(查年, 查月, 查日)),//DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day
            年 = 微软农历.GetSexagenaryYear(DateTime.Parse(查年.ToString("0年") + 查月.ToString("0月") + 查日.ToString("0日")))
            , 闰月 = 0;
            if (微软农历.IsLeapYear(查年)) while (微软农历.IsLeapMonth(查年, ++闰月) == false && 闰月 < 12);
            if (微软农历.IsLeapYear(查年 - 1)) while (微软农历.IsLeapMonth(查年 - 1, ++闰月) == false && 闰月 < 12);

            if ((微软农历.IsLeapYear(查年) && 月 >= 闰月 && 月 < 12) || (微软农历.IsLeapYear(查年 - 1) && (月 == 12 || 月 == 13)))
                --月;

            return "月历:" + " 甲乙丙丁戊己庚辛壬癸"[微软农历.GetCelestialStem(年)]
                + " 鼠牛虎兔龙蛇马羊猴鸡狗猪"[微软农历.GetTerrestrialBranch(年)] + "年"
                + " 春梅桃麦仲荷瓜桂菊露冬腊"[月] + (天 > 10 ? "月" : "月初") + (" 十廿仨"[天 / 10]).ToString().Replace(" ", "")
                + " 一二三四五六七八九"[天 % 10];//〇 
        }
        static int 返回月的天数(int N, int Y)
        {//缘由https://bbs.csdn.net/topics/395074486
            return (Y == 2 ? (((((N % 4) == 0 && (N % 100) > 0) || (N % 400) == 0) ? true : false) ? 29 : 28)
                : (((Y <= 7 && (Y % 2) > 0) || (Y > 7 && (Y % 2) == 0)) ? 31 : 30));
        }
        static void 输出万年历(int 年, int 月, int 日)
        {
            int r = 1, z = r, nn = 年, yy = 月; string aa = "  :::: ", ny = "";
            Func<int, string> 补零 = (int 数据) => { return (数据 > 9 ? "" : "0"); };
            while (nn > 0) if (--yy > 0) z += 返回月的天数(nn, yy); else { --nn; yy = 13; }
            nn = 返回月的天数(年, 月); yy = 1; z %= 7;
            //Console.WriteLine("{0}", 月历加载());
            Console.Write("{0}年" + 补零(月) + "{1}月" + 补零(日) + "{2}日星期{3}  {4}\n天 一 二 三 四 五 六\n:: ", 年, 月, 日, "天一二三四五六"[(z + 日 - 1) % 7], ny = 月历加载(年, 月, 日));
            while (r <= nn)
                if (yy >= z)
                {
                    var dd = 月历加载(年, 月, r);
                    dd = dd.Replace("初一", dd.Substring(6, 1)+"一");
                    if (r <= 返回月的天数(年, 月)) aa += dd.Substring(8) + ((++yy % 7) > 0 ? " " : " \n");
                    Console.Write("{0}{1}{2}", 补零(r), (r++).ToString(), " ");
                    if (r > nn || aa.Contains("\n"))
                    {
                        if (r > nn)
                        {
                            if (yy % 7 == 0)
                            {
                                if (aa != "  ")
                                {
                                    Console.Write("{0}", aa = aa.Replace("  ", " ").Replace("廿 ", "廿〇 ").Replace("仨 ", "仨〇 "));
                                    aa = "  ";
                                }
                            }
                            while (yy++ < 42)
                            {
                                Console.Write("{0}", ":: ");
                                if (yy % 7 == 0)
                                {
                                    if (aa != "  ")
                                    {
                                        Console.Write("{0}", aa = aa.Replace("  ", " ").Replace("廿 ", "廿〇 ").Replace("仨 ", "仨〇 "));
                                        aa = "  ";
                                    }
                                    else
                                        Console.Write(" ::::::::::::::::::::::::::::::::::");
                                    Console.WriteLine();
                                }
                            }
                        }
                        Console.Write("{0}", aa = aa.Replace("  ", " ").Replace("廿 ", "廿〇 ").Replace("仨 ", "仨〇 "));
                        aa = "  ";
                    }
                }
                else
                {
                    if (r <= 返回月的天数(年, 月)) aa += (r > 1 ? 月历加载(年, 月, r).Substring(8) : "::::") + ((yy % 7) > 0 ? " " : " \n");
                    Console.Write("{0}{1}{2}", (r > 1 ? 补零(r) : ""), (r > 1 ? (r++).ToString() : "::"), ((++yy % 7) > 0 ? " " : " \n"));
                    if (aa.Contains("\n")) { Console.Write("{0}", aa = aa.Replace("  ", " ").Replace("廿 ", "廿〇 ").Replace("仨 ", "仨〇 ")); aa = "  "; }
                }
            Console.WriteLine();
        }

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐