ASP.NET显示农历时间改进版
网络编程 2021-07-04 22:41www.168986.cn编程入门
这篇文章主要介绍了ASP.NET显示农历时间改进版,是针对前面一篇ASP.NET显示农历时间的改进版,实现了比较简单的封装,增加了易用性,非常具有实用价值,需要的朋友可以参考下
本文实例讲述了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];
}
}
{
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;
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();//公历日期
string ChineseTimeNow = cd.GetChineseDate(DateTime.Now);//农历日期
string ForignTimeNow = DateTime.Now.GetDateTimeFormats('D')[0].ToString();//公历日期
狼蚁网站SEO优化有一个测试的效果
前台代码
代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestCountryDate._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://.w3./TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://.w3./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>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://.w3./TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://.w3./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;
}
}
{
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.程序设计有所帮助。
上一篇:ASP.NET MVC小结之基础篇(一)
下一篇:ASP.NET显示农历时间的方法
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程