Asp. NET display Chinese lunar time (two)

Source: Internet
Author: User

There is an article in front of the lunar time (http://blog.csdn.net/yysyangyangyangshan/article/details/6802950), but it is not packaged and needs to be manually modified. This is a simple package that can be called directly.

The code is as follows:

Class for taking Chinese lunar time

   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 = "Methyl-n-Xing";        Public Const string Terrestrialbranch = "Obscure Chen has not Shin Hai"; public static readonly string[] Chinesedayname = new string[] {"First", "Second day", "Third Day", "four", "Duanwu", "Arrest", "When初七because", "Holidays", "Penggushan", "initial Ten "," 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>///Get a Gregorian date corresponding to the full lunar Date///</summary>//<param Name= "Time" > a Gregorian date &l t;/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>///For a Gregorian calendar date Ganzhi///</summary>//<param Name= "Time" > A        Gregorian date </param>///<returns> lunar Ganzhi calendar </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>///For a Gregorian calendar year///</summary>//<param Name= "Time" > one Gregorian 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>///For the Lunar calendar month///</summary>//<param Name= "Time" > one Gregorian 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; The lunar month cannot leap month 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?)        "Leap": "") + chinesemonthname[month-1]; }///<summary>///For a Gregorian calendar day///</summary>//<param Name= "Time" > One calendar day            Period </param>///<returns> Lunar day </returns> public string GetDay (DateTime time) { Return Chinesedayname[calendar.        GetDayOfMonth (Time)-1]; }    }


The required using

Using system;using system.collections.generic;using system.web;using system.text;using System.Globalization;

Call:

            Countrydate cd = new Countrydate ();            String chinesetimenow = CD. Getchinesedate (DateTime.Now);//Lunar date            string forigntimenow = DateTime.Now.GetDateTimeFormats (' D ') [0]. ToString ();//Gregorian date


Here is the effect of a test:

Front Code:

<%@ 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 ">


Background code:

    public partial class _default:system.web.ui.page    {        protected void Page_Load (object sender, EventArgs e)        { c4/>}        protected void Button1_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 ();//Gregorian date            lblcountrydate.text = Chinesetimenow;            Lblforigndate.text = Forigntimenow;        }    }


The effect is as follows:

The main time to take is this countrydate class, call to take time. If there is a better optimization welcome everyone to communicate.

Asp. NET display Chinese lunar time (two)

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.