zoukankan      html  css  js  c++  java
  • 获取农历日期

    http://m.jdd.blog.163.com/blog/static/52525241200992092344168/

    http://hi.baidu.com/zonecens/blog/item/5a94be8f7c8d89f2503d9266.html

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    using System.Collections.Generic;
    using System.Text;

    using System.Globalization;


    public partial class TestDate : System.Web.UI.Page
    {
        private static ChineseLunisolarCalendar calendar = new ChineseLunisolarCalendar();
        private static string ChineseNumber = "〇一二三四五六七八九";
        public const string CelestialStem = "甲乙丙丁戊己庚辛壬癸";
        public const string TerrestrialBranch = "子丑寅卯辰巳午未申酉戌亥";
        public static readonly string[] ChineseDayName = new string[] {
                "初一","初二","初三","初四","初五","初六","初七","初八","初九","初十",
                "十一","十二","十三","十四","十五","十六","十七","十八","十九","二十",
                "廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十"};
        public static readonly string[] ChineseMonthName = new string[] { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "腊" };

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write(DateTime.Now.ToString()+"<br>");
            Response.Write(GetChineseDate(DateTime.Now));

        }
        private void Transform(DateTime dt)
        {
           
        }
        /// <summary>
        /// 获取一个公历日期对应的完整的农历日期
        /// </summary>
        /// <param name="time">一个公历日期</param>
        /// <returns>农历日期</returns>
        public string GetChineseDate(DateTime time)
        {
            string strY = GetYear(time);
            string strM = GetMonth(time);
            string strD = GetDay(time);
            string strSB = GetStemBranch(time);
            string strDate = strY + "(" + strSB + ")年 " + strM + "月 " + strD;
            return strDate;
        }
        /// <summary>
        /// 获取一个公历日期的农历干支纪年
        /// </summary>
        /// <param name="time">一个公历日期</param>
        /// <returns>农历干支纪年</returns>
        public string GetStemBranch(DateTime time)
        {
            int sexagenaryYear = calendar.GetSexagenaryYear(time);
            string stemBranch = CelestialStem.Substring(sexagenaryYear % 10 - 1, 1) + TerrestrialBranch.Substring(sexagenaryYear % 12 - 1, 1);
            return stemBranch;
        }

        /// <summary>
        /// 获取一个公历日期的农历年份
        /// </summary>
        /// <param name="time">一个公历日期</param>
        /// <returns>农历年份</returns>
        public string GetYear(DateTime time)
        {
            StringBuilder sb = new StringBuilder();
            int year = calendar.GetYear(time);
            int d;
            do
            {
                d = year % 10;
                sb.Insert(0, ChineseNumber[d]);
                year = year / 10;
            } while (year > 0);
            return sb.ToString();
        }


        /// <summary>
        /// 获取一个公历日期的农历月份
        /// </summary>
        /// <param name="time">一个公历日期</param>
        /// <returns>农历月份</returns>
        public string GetMonth(DateTime time)
        {
            int month = calendar.GetMonth(time);
            int year = calendar.GetYear(time);
            int leap = 0;

            //正月不可能闰月
            for (int i = 3; i <= month; i++)
            {
                if (calendar.IsLeapMonth(year, i))
                {
                    leap = i;
                    break; //一年中最多有一个闰月
                }

            }
            if (leap > 0) month--;
            return (leap == month + 1 ? "闰" : "") + ChineseMonthName[month - 1];
        }


        /// <summary>
        /// 获取一个公历日期的农历日
        /// </summary>
        /// <param name="time">一个公历日期</param>
        /// <returns>农历日</returns>
        public string GetDay(DateTime time)
        {
            return ChineseDayName[calendar.GetDayOfMonth(time) - 1];
        }
    }

  • 相关阅读:
    DirectX编译环境配置
    [转]unresolved external symbol __imp__PlaySoundA@12的解决方法
    Win7/Win8/Win10显示桌面按钮
    Win8中更改账户信息
    3D游戏图形引擎
    【整理】鼠标位置编码(Mouse Position Code)和鼠标激活返回值(MOUSEACTIVATE Return Codes)
    精简ICO图标可减小EXE程序文件大小
    【整理】SYSCOMMAND的wParam值的宏定义
    迅雷网速测试器 下载速率测试记录
    【整理】窗体消息(Window Messages) 的宏定义
  • 原文地址:https://www.cnblogs.com/kenryuu/p/1629484.html
Copyright © 2011-2022 走看看