ASP.NET显示农历时间的方法-创新互联
本篇内容介绍了“ASP.NET显示农历时间的方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
创新互联建站长期为上1000家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为镇远企业提供专业的网站设计、网站建设,镇远网站改版等技术服务。拥有10年丰富建站经验和众多成功案例,为您定制开发。取农历时间的类
代码如下:
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[] { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二" };
///
/// 获取一个公历日期对应的完整的农历日期
///
/// 一个公历日期
///
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;
}
///
/// 获取一个公历日期的农历干支纪年
///
/// 一个公历日期
///
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;
}
///
/// 获取一个公历日期的农历年份
///
/// 一个公历日期
///
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();
}
///
/// 获取一个公历日期的农历月份
///
/// 一个公历日期
///
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];
}
///
/// 获取一个公历日期的农历日
///
/// 一个公历日期
///
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" %>
后台代码:
复制代码 代码如下:
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显示农历时间的方法”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!
当前标题:ASP.NET显示农历时间的方法-创新互联
URL分享:http://myzitong.com/article/ceosec.html