본문 바로가기
개발/C#

c# 양력 -> 음력 변환

by 혈중마라농도 2021. 12. 5.
namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            // 양력 -> 음력
            DateTime date = new DateTime(2021, 2, 17);
            Console.WriteLine($"DateFormat: {ToLunarDateTime(date).ToString("yyyy-MM-dd")}");
           
        }

        // 양력 -> 음력 변환
        public static DateTime ToLunarDateTime(DateTime date)
        {
            int leapMonth;
            int lunarYear;
            int lunarMonth;
            int lunarDay;
            System.Globalization.KoreanLunisolarCalendar lunar = new System.Globalization.KoreanLunisolarCalendar();

            lunarYear = lunar.GetYear(date);
            lunarMonth = lunar.GetMonth(date);
            lunarDay = lunar.GetDayOfMonth(date);
            if (lunar.GetMonthsInYear(lunarYear) > 12)
            {
                leapMonth = lunar.GetLeapMonth(lunarYear);
                if (lunarMonth >= leapMonth){
                    lunarMonth--;
                }
            }
            return new DateTime(lunarYear, lunarMonth, lunarDay);
        }

    }
}
반응형

댓글