ASP. NET: displays the Chinese calendar time Ultimate Edition and asp.net lunar calendar Ultimate Edition.
The example in this article describes how ASP. NET displays the lunar calendar time. It is the simplified version of the source code of the previous article. Share it with you for your reference. The details are as follows:
In the previous article, we used the Chinese calendar time http://www.bkjia.com/article/57481.htm. the manual modification was not started. This is a simple encapsulation and can be called directly.
The Code is as follows:
Category of the lunar calendar time
Copy codeThe Code is as follows: public class CountryDate
{
Public string ChineseTimeNow = "";
Public string ForignTimeNow = "";
Private static ChineseLunisolarCalendar calendar = new ChineseLunisolarCalendar ();
Private static string ChineseNumber = "1234 ";
Public const string CelestialStem = "A, B, C, E, Ji, Geng, Xin, Xi ";
Public const string TerrestrialBranch = "zi ";
Public static readonly string [] ChineseDayName = new string [] {
"First Day", "Second Day", "Third Day", "fourth day", "Fifth Day", "Sixth Day", "Seventh Day", "Eighth Day", "Ninth Day", "tenth day ",
"11", "12", "13", "14", "15", "16", "17", "18", "19", "20 ",
"Jun 1", "Jun 2", "Jun 3", "Jun 4", "Jun 5", "Jun 6", "Jun 7", "Jun 8 ", "9th", "30th "};
Public static readonly string [] ChineseMonthName = new string [] {"zheng", "two", "three", "four", "five", "Six ", "7", "8", "9", "10", "11", "12 "};
/// <Summary>
/// Obtain the complete lunar date corresponding to a calendar date
/// </Summary>
/// <Param name = "time"> one calendar date </param>
/// <Returns> lunar date </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 + ") Year" + strM + "month" + strD;
Return strDate;
}
/// <Summary>
/// Obtain the calendar year for a calendar date
/// </Summary>
/// <Param name = "time"> one calendar date </param>
/// <Returns> calendar year </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>
/// Obtain the year of the calendar year of a calendar date
/// </Summary>
/// <Param name = "time"> one calendar date </param>
/// <Returns> lunar year </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>
/// Obtain the calendar month of a calendar date
/// </Summary>
/// <Param name = "time"> one calendar date </param>
/// <Returns> lunar month </returns>
Public string GetMonth (DateTime time)
{
Int month = calendar. GetMonth (time );
Int year = calendar. GetYear (time );
Int leap = 0;
// It cannot be a leap month in lunar January
For (int I = 3; I <= month; I ++)
{
If (calendar. IsLeapMonth (year, I ))
{
Leap = I;
Break; // up to one leap month in a year
}
}
If (leap> 0) month --;
Return (leap = month + 1? "Region": "") + ChineseMonthName [month-1];
}
/// <Summary>
/// Obtain the calendar day of a calendar date
/// </Summary>
/// <Param name = "time"> one calendar date </param>
/// <Returns> lunar day </returns>
Public string GetDay (DateTime time)
{
Return ChineseDayName [calendar. GetDayOfMonth (time)-1];
}
}
Required using
Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Web;
Using System. Text;
Using System. Globalization;
Call:
Copy codeThe Code is as follows: CountryDate cd = new CountryDate ();
String ChineseTimeNow = cd. GetChineseDate (DateTime. Now); // lunar date
String ForignTimeNow = DateTime. Now. GetDateTimeFormats ('D') [0]. ToString (); // calendar date
The following is a test result:
Front-end code:
Copy codeThe Code is as follows: <% @ 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 = "calendar time"/> </td>
<Td> <asp: Label ID = "lblForignDate" runat = "server"/> </td>
</Tr>
</Table>
<Asp: Button ID = "buttton1" runat = "server" Text = "display time" OnClick = "button#click"/>
</Div>
</Form>
</Body>
</Html>
Background code:
Copy codeThe Code is as follows: public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
}
Protected void button#click (object sender, EventArgs e)
{
CountryDate cd = new CountryDate ();
String ChineseTimeNow = cd. GetChineseDate (DateTime. Now); // lunar date
String ForignTimeNow = DateTime. Now. GetDateTimeFormats ('D') [0]. ToString (); // calendar date
LblCountryDate. Text = ChineseTimeNow;
LblForignDate. Text = ForignTimeNow;
}
}
Shows the running effect:
The main time is the CountryDate class. You can call it to obtain the time.
I hope this article will help you design your asp.net program.