System.Globalization.TaiwanLunisolarCalendar.GetEra C# (CSharp) Method

GetEra() public method

public GetEra ( System.DateTime time ) : int
time System.DateTime
return int
        public override int GetEra(DateTime time) {
            return (helper.GetEra(time));
        }

Usage Example

        /// <summary>
        /// 取得目前要使用的樣板
        /// </summary>
        /// <param name="beforeDay">前幾天(節慶)</param>
        /// <returns>目前要使用的樣板</returns>
        public TemplateVO GetCurrentTemplate(int beforeDay)
        {
            //先抓節日的 (要存農曆日還是西元要再確認,存西元必須每年度去調整)
            IList<TemplateVO> festivalTemplates = GetTemplateList(TemplateVO.Type.Festival);
            if (festivalTemplates != null && festivalTemplates.Count > 0)
            {
                foreach (TemplateVO templateVO in festivalTemplates)
                {
                    if (templateVO.Name.Equals("聖誕節") || templateVO.Name.Equals("西洋情人節"))
                    {
                        //此兩個節日用西元算
                        DateTime today = DateTime.Today;
                        int year = DateTime.Today.Year;
                        int month = int.Parse(templateVO.StartDate.Substring(0, 2));
                        int day = int.Parse(templateVO.StartDate.Substring(2, 2));
                        DateTime endDate = new DateTime(DateTime.Today.Year, month, day);
                        DateTime startDate = endDate.AddDays(((-1) * beforeDay));

                        if (today >= startDate && today <= endDate)
                        {
                            return templateVO;
                        }
                    }
                    else
                    {
                        //此其他的節日用農曆算

                        //先從農曆轉成西元
                        TaiwanLunisolarCalendar tlc = new TaiwanLunisolarCalendar();
                        DateTime today = DateTime.Today;
                        int year = tlc.GetYear(today);
                        int month = int.Parse(templateVO.StartDate.Substring(0, 2));
                        int day = int.Parse(templateVO.StartDate.Substring(2, 2));
                        int era = tlc.GetEra(today);

                        //判斷今年是否為閏年,閏年的話抓出正確的日期
                        if (tlc.IsLeapYear(year, era))
                        {
                            //閏月月份
                            int leapMonth = tlc.GetLeapMonth(year, era);
                            //閏月天數
                            //int monthDays = tlc.GetDaysInMonth(year, leapMonth, era);

                            //閏月有13個月,例如leapMonth=5,代表閏四月,則農曆5月開始要多加1個月
                            if (month >= leapMonth)
                            {
                                month += 1;
                            }
                        }

                        DateTime endDate = tlc.ToDateTime(year, month, day, 0, 0, 0, 0, era);

                        DateTime startDate = endDate.AddDays(((-1) * beforeDay));

                        if (today >= startDate && today <= endDate)
                        {
                            return templateVO;
                        }
                    }
                }
            }

            //季節
            IList<TemplateVO> seasonTemplates = GetTemplateList(TemplateVO.Type.Season);
            if (seasonTemplates != null && seasonTemplates.Count > 0)
            {
                DateTime date = DateTime.Today.AddDays(beforeDay);
                string showDate = string.Format("{0}{1}", date.Month.ToString().PadLeft(2, '0'), date.Day.ToString().PadLeft(2, '0'));
                int count = 0;
                foreach (TemplateVO templateVO in seasonTemplates)
                {
                    count += 1;
                    if (count < seasonTemplates.Count)
                    {
                        if (int.Parse(showDate) >= int.Parse(templateVO.StartDate) && int.Parse(showDate) <= int.Parse(templateVO.EndDate))
                        {
                            return templateVO;
                        }
                    }
                    else
                    {
                        //最後一季的算法不一樣,可能有跨年(起始日期>結束時 則表示有跨年)
                        if (int.Parse(templateVO.StartDate) > int.Parse(templateVO.EndDate))
                        {
                            if (int.Parse(showDate) >= int.Parse(templateVO.StartDate) && int.Parse(showDate) <= int.Parse("1231"))
                            {
                                return templateVO;
                            }
                            if (int.Parse(showDate) >= int.Parse("0101") && int.Parse(showDate) <= int.Parse(templateVO.EndDate))
                            {
                                return templateVO;
                            }
                        }
                        else
                        {
                            if (int.Parse(showDate) >= int.Parse(templateVO.StartDate) && int.Parse(showDate) <= int.Parse(templateVO.EndDate))
                            {
                                return templateVO;
                            }
                        }
                    }
                }
            }

            //都沒有的話回傳季節的第一個樣板
            return seasonTemplates[0];
        }
All Usage Examples Of System.Globalization.TaiwanLunisolarCalendar::GetEra