C#實現計算今天是農曆幾號的代碼

來源:互聯網
上載者:User

代碼說明:

1.說明:在東亞各國,除了通用的公元紀年之外,還有各自以前使用的陰曆紀年法,在.net2.0種增加了針對東亞各國的日曆類EastAsianLunisolarCalendar,

2.它是一個抽象類別,有各種針對不同國家的的子類,其中ChineseLunisolarCalendar就是針對中國的日曆類,它提公元紀年與中國傳統農曆紀年之間的相互轉換,並能計算今天是農曆幾月幾日星期幾。

3.利用它可以計算天干地支等有關農曆的資訊,本程式就是來簡單展示這個類的用法。它能計算的農曆範圍從西曆1901-2-19至2101-1-28。

using System;   
using System.Collections.Generic;   
using System.Text;   
using System.Globalization;  
namespace ChineseCalendar   
{   
    public class Calendar   
    {   
        private static ChineseLunisolarCalendar chineseDate = new ChineseLunisolarCalendar();   
        static void Main(string[] args)   
        {   
            //ChineseLunisolarCalendar chineseDate = new ChineseLunisolarCalendar();   
            ShowYearInfo();   
            ShowCurrentYearInfo();   
            Console.ReadLine();   
        }   
        /// <summary>   
        /// 展示陰曆年份資訊   
        /// </summary>   
        public static void ShowYearInfo()   
        {   
            for (int i = chineseDate.MinSupportedDateTime.Year; i < chineseDate.MaxSupportedDateTime.Year; i++)   
            {   
                Console.WriteLine("年份:{0},月份總數:{1},總天數:{2},幹支序號:{3}", i, chineseDate.GetMonthsInYear(i),chineseDate.GetDaysInYear(i)   
                                  ,chineseDate.GetSexagenaryYear(new DateTime(i,3,1)));   
            }   
        }   
        /// <summary>   
        /// 展示當前年份資訊   
        /// </summary>   
        public static void ShowCurrentYearInfo()   
        {   
            int lYear=chineseDate.GetYear(DateTime.Now);   
            int lMonth=chineseDate.GetMonth(DateTime.Now);   
            int lDay=chineseDate.GetDayOfMonth(DateTime.Now);                 
            int leapMonth = chineseDate.GetLeapMonth(lYear);//擷取第幾個月是閏月,等於0表示本年無閏月   
               
            //如果今年有閏月   
            if (leapMonth > 0)   
            {   
                //閏月數等於當前月份   
                if (lMonth == leapMonth)   
                {   
                    Console.WriteLine("今年的陰曆日期:{0}年閏{1}月{2}日。", lYear, lMonth - 1, lDay);   
                }   
                else if (lMonth > leapMonth)//   
                {   
                    Console.WriteLine("今年的陰曆日期:{0}年{1}月{2}日。", lYear, lMonth - 1, lDay);   
                }   
                else  
                {   
                    Console.WriteLine("今年的陰曆日期:{0}年{1}月{2}日。", lYear, lMonth, lDay);   
                }   
                   
            }   
            else  
            {   
                Console.WriteLine("今年的陰曆日期:{0}年{1}月{2}日。", lYear, lMonth, lDay);   
            }   
            Console.WriteLine("今天的西曆日期:" + DateTime.Now.ToString("yyyy-MM-dd"));   
            Console.WriteLine("今年陰曆天數:{0},今年{1}閏年", chineseDate.GetDaysInYear(DateTime.Now.Year),(chineseDate.IsLeapYear(DateTime.Now.Year)==true)?"是":"不是");   
               
            Console.WriteLine("今年農曆每月的天數:");//注意:如果有13個數字表示當年有閏月   
            for (int i = 1; i <= chineseDate.GetMonthsInYear(DateTime.Now.Year); i++)   
            {   
                Console.Write("{0,-5}",chineseDate.GetDaysInMonth(DateTime.Now.Year,i));   
            }   
        }   
    }   

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.