C#根据指定日期计算农历

2 篇文章 0 订阅
1 篇文章 0 订阅

在Exit.cs类中
引用using System.Globalization;

public class Exit
{
    ChineseLunisolarCalendar cCalendar = new ChineseLunisolarCalendar();

 /// <summary>
    /// 根据公历获取农历日期
    /// </summary>
    /// <param name="datetime">公历日期</param>
    /// <returns></returns>
    public string GetChineseDateTime(DateTime datetime)
    {
        int lyear = cCalendar.GetYear(datetime);
        int lmonth = cCalendar.GetMonth(datetime);
        int lday = cCalendar.GetDayOfMonth(datetime);

        //获取闰月, 0 则表示没有闰月
        int leapMonth = cCalendar.GetLeapMonth(lyear);

        bool isleap = false;

        if (leapMonth > 0)
        {
            if (leapMonth == lmonth)
            {
                //闰月
                isleap = true;
                lmonth--;
            }
            else if (lmonth > leapMonth)
            {
                lmonth--;
            }
        }

        return string.Concat(GetLunisolarYear(lyear), "年", isleap ? "闰" : string.Empty, GetLunisolarMonth(lmonth), "月", GetLunisolarDay(lday));
    }
    #region 农历年

    /// <summary>
    /// 十天干
    /// </summary>
    private static string[] tiangan = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };

    /// <summary>
    /// 十二地支
    /// </summary>
    private static string[] dizhi = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };

    /// <summary>
    /// 十二生肖
    /// </summary>
    private static string[] shengxiao = { "鼠", "牛", "虎", "免", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };


    /// <summary>
    /// 返回农历天干地支年 
    /// </summary>
    /// <param name="year">农历年</param>
    /// <returns></returns>
    public static string GetLunisolarYear(int year)
    {
        if (year > 3)
        {
            int tgIndex = (year - 4) % 10;
            int dzIndex = (year - 4) % 12;

            return string.Concat(tiangan[tgIndex], dizhi[dzIndex], "[", shengxiao[dzIndex], "]");

        }

        throw new ArgumentOutOfRangeException("无效的年份!");
    }


    #endregion


    #region 农历月

    /// <summary>
    /// 农历月
    /// </summary>
    private static string[] months = { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "腊" };


    /// <summary>
    /// 返回农历月
    /// </summary>
    /// <param name="month">月份</param>
    /// <returns></returns>
    public static string GetLunisolarMonth(int month)
    {
        if (month < 13 && month > 0)
        {
            return months[month - 1];
        }

        throw new ArgumentOutOfRangeException("无效的月份!");
    }


    #endregion


    #region 农历日

    /// <summary>
    /// 
    /// </summary>
    private static string[] days1 = { "初", "十", "廿", "三" };

    /// <summary>
    /// 日
    /// </summary>
    private static string[] days = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };


    /// <summary>
    /// 返回农历日
    /// </summary>
    /// <param name="day"></param>
    /// <returns></returns>
    public static string GetLunisolarDay(int day)
    {
        if (day > 0 && day < 32)
        {
            if (day != 20 && day != 30)
            {
                return string.Concat(days1[(day - 1) / 10], days[(day - 1) % 10]);
            }
            else
            {
                return string.Concat(days[(day - 1) / 10], days1[1]);
            }
        }

        throw new ArgumentOutOfRangeException("无效的日!");
    }

    #endregion
    
}

在Exit_Handler .ashx一般处理程序中

public class Exit_Handler : IHttpHandler 
{
    Exit et = new Exit();

    public void ProcessRequest(HttpContext context)
    {
        var action = context.Request["action"];
        switch (action)
        {
            case "GetChineseDateTime":
                GetChineseDateTime(context);
                break;
            default:
                break;
     	}
     }

private void GetChineseDateTime(HttpContext context)
    {
        DateTime datetime = Convert.ToDateTime(context.Request["datetime"]);
        string str = et.GetChineseDateTime(datetime);
        context.Response.Write(str);
    }
}


通过ajax访问

					mui.ajax({
                        url: 'Exit_Handler.ashx?action=GetChineseDateTime',
                        type: 'post',
                        async: false,
                        data: {
                            "datetime": '2021-07-29'
                        },
                        success: function (data) {
                            console.log(date)
                        }
                    })
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值