当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET显示农历时间改进版

ASP.NET显示农历时间改进版

2017年12月12日  | 移动技术网IT编程  | 我要评论

瑞秋尼科尔斯,张小旺,柯景腾扮演者

本文实例讲述了asp.net显示农历时间的方法,是前面一篇文章源码的改进版。分享给大家供大家参考。具体如下:

前面有一篇取农历时间,不过没有进行封装使用起来需要手动修改。本次进行简单封装一下,可以直接进行调用。

代码如下:

取农历时间的类

复制代码 代码如下:
public class countrydate 

     public string chinesetimenow = ""; 
     public string forigntimenow = ""; 
     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[] { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二" }; 
 
     /// <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]; 
     } 
}

需要的using

复制代码 代码如下:
using system; 
using system.collections.generic; 
using system.web; 
using system.text; 
using system.globalization;

调用:
复制代码 代码如下:
countrydate cd = new countrydate(); 
string chinesetimenow = cd.getchinesedate(datetime.now);//农历日期 
string forigntimenow = datetime.now.getdatetimeformats('d')[0].tostring();//公历日期

下面有一个测试的效果:

前台代码:

复制代码 代码如下:
<%@ page language="c#" autoeventwireup="true" codebehind="default.aspx.cs" inherits="testcountrydate._default" %> 
 
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
    <table> 
     <tr> 
      <td><asp:label id="label1" runat="server" text="农历时间"/></td> 
      <td><asp:label id="lblcountrydate" runat="server"/></td> 
     </tr> 
     <tr> 
      <td><asp:label id="label2" runat="server" text="公历时间"/></td> 
      <td><asp:label id="lblforigndate" runat="server"/></td> 
     </tr> 
    </table> 
    <asp:button id="buttton1" runat="server" text="显示时间" onclick="button1_click" /> 
    </div> 
    </form> 
</body> 
</html>

后台代码:

复制代码 代码如下:
public partial class _default : system.web.ui.page 

    protected void page_load(object sender, eventargs e) 
    { 
 
    } 
 
    protected void button1_click(object sender, eventargs e) 
    { 
        countrydate cd = new countrydate(); 
        string chinesetimenow = cd.getchinesedate(datetime.now);//农历日期 
        string forigntimenow = datetime.now.getdatetimeformats('d')[0].tostring();//公历日期 
 
        lblcountrydate.text = chinesetimenow; 
        lblforigndate.text = forigntimenow; 
    } 
}

运行效果如下图所示:

主要取时间就是这个countrydate类,调用取时间即可。

希望本文所述对大家的asp.net程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网