Asp. NET show Chinese lunar Time Improved version _ Practical skills

Source: Internet
Author: User

The example of this article tells the asp.net to display the lunar time, is an improved version of the previous article source. Share to everyone for your reference. Specifically as follows:

There is a http://www.jb51.net/article/57481.htm in front of the lunar calendar, but no encapsulation is used to manually modify. This is a simple encapsulation, you can call directly.

The code is as follows:

Class to take the lunar time

Copy Code code as follows:
public class Countrydate
{
public string chinesetimenow = "";
public string forigntimenow = "";
private static Chineselunisolarcalendar calendar = new Chineselunisolarcalendar ();
private static string Chinesenumber = "0 123456789";
Public Const string Celestialstem = "A B-butyl Xing";
Public Const string Terrestrialbranch = "Kaniko chen has not Shin Hai";
public static readonly string[] Chinesedayname = new string[] {
"The Junior", "the Junior", "the Junior", "Hansi", "Duanwu", "Arrest", "When初七because", "Day", "Yanji", "decade",
"11", "12", "13", "14", "XV", "16", "17", "18", "19", "20",
"21", "22", "23", "24", "25", "26", "27", "28", "29", "30"};
public static readonly string[] Chinesemonthname = new string[] {"Positive", "two", "three", "four", "five", "six", "seven", "eight", "Nine", "Ten", "11", "12"};

<summary>
Gets the full lunar date for a Gregorian date
</summary>
<param name= "Time" > a Gregorian calendar date </param>
<returns> Lunar Date </returns>
public string getchinesedate (DateTime)
{
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>
Get a Gregorian calendar date of the lunar Ganzhi Annals
</summary>
<param name= "Time" > a Gregorian calendar date </param>
<returns> Lunar Ganzhi Annals </returns>
public string Getstembranch (DateTime)
{
int sexagenaryyear = Calendar. Getsexagenaryyear (time);
String stembranch = celestialstem.substring (sexagenaryyear% 10-1, 1) + terrestrialbranch.substring (sexagenaryYear% 12 -1, 1);
return stembranch;
}

<summary>
Gets the lunar year of a Gregorian date
</summary>
<param name= "Time" > a Gregorian calendar date </param>
<returns> Lunar Year </returns>
public string getyear (DateTime)
{
StringBuilder sb = new StringBuilder ();
int year = Calendar. GetYear (time);
int D;
Todo
{
d = year% 10;
Sb. Insert (0, chinesenumber[d]);
Year = YEAR/10;
while (year > 0);
Return SB. ToString ();
}

<summary>
Gets the lunar month of a Gregorian date
</summary>
<param name= "Time" > a Gregorian calendar date </param>
<returns> Lunar month </returns>
public string getmonth (DateTime)
{
int month = Calendar. GetMonth (time);
int year = Calendar. GetYear (time);
int leap = 0;

The lunar month cannot leap month
for (int i = 3; I <= month; i++)
{
if (Calendar. Isleapmonth (year, i))
{
leap = i;
Break There is up to one leap month in a year
}

}
if (Leap > 0) month--;
return (Leap = = month + 1?) "Leap": "" "+ chinesemonthname[month-1];
}

<summary>
Get the lunar day of a Gregorian date
</summary>
<param name= "Time" > a Gregorian calendar date </param>
<returns> Lunar Day </returns>
public string getday (DateTime)
{
Return Chinesedayname[calendar. GetDayOfMonth (Time)-1];
}
}

The required using

Copy Code code as follows:
Using System;
Using System.Collections.Generic;
Using System.Web;
Using System.Text;
Using System.Globalization;

Call:
Copy Code code as follows:
Countrydate cd = new Countrydate ();
String chinesetimenow = CD. Getchinesedate (DateTime.Now);//Lunar calendar date
String forigntimenow = DateTime.Now.GetDateTimeFormats (' D ') [0]. ToString ();//Gregorian date

Here's the effect of a test:

Foreground code:

Copy Code code 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 ">

<title></title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<table>
<tr>
<td><asp:label id= "Label1" runat= "server" text= "Lunar Time"/></td>
<td><asp:label id= "lblcountrydate" runat= "Server"/></td>
</tr>
<tr>
<td><asp:label id= "Label2" runat= "Server" text= "Gregorian calendar Time"/></td>
<td><asp:label id= "lblforigndate" runat= "Server"/></td>
</tr>
</table>
<asp:button id= "Buttton1" runat= "Server" text= "Show Time" onclick= "Button1_Click"/>
</div>
</form>
</body>

Background code:

Copy Code code as follows:
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);//Lunar calendar date
String forigntimenow = DateTime.Now.GetDateTimeFormats (' D ') [0]. ToString ();//Gregorian date

Lblcountrydate.text = Chinesetimenow;
Lblforigndate.text = Forigntimenow;
}
}

The effect is as shown in the following illustration:

The main time is this countrydate class, call take time.

I hope this article will help you with the ASP.net program design.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.