The previous article takes the lunar calendar time (http: blog. csdn. netyysyangyangyangyshanarticledetails6802950), but it needs to be manually modified if it is not encapsulated and used. This is a simple encapsulation and can be called directly. The code is as follows: obtain the publicclassCountryDate class of the lunar date {
The previous article took the lunar date (http://blog.csdn.net/yysyangyangyangshan/article/details/6802950), but was not encapsulated for use and needs to be manually modified. This is a simple encapsulation and can be called directly.
The code is as follows:
Category of the lunar calendar time
Public class CountryDate {public string ChineseTimeNow = ""; public string ForignTimeNow = ""; private static variable calendar = new ChineseLunisolarCalendar (); private static string ChineseNumber = "1234 "; public const string CelestialStem = ""; public const string TerrestrialBranch = ""; 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", "Eleventh Day", "Twelfth Day", "Thirteen", "Fourteen", "Tenth Day", "Sixteen ", "17", "18", "19", "20", "Jun 1", "Jun 2", "Jun 3", "Jun 4", "Jun 5 ", "Sixty-six", "Fifty-seven", "Fifty-eight", "Fifty-nine", "thirty "}; public static readonly string [] ChineseMonthName = new string [] {"zheng", "two", "three", "four", "five", "six ", "7", "8", "9", "10", "11", "12 "};////// Obtain the complete lunar date corresponding to a calendar date //////One calendar date///
Lunar calendar date
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 ;}////// Obtain the calendar year for a calendar date //////One calendar date///
Calendar year
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 ;}////// Obtain the calendar year of a calendar date //////One calendar date///
Lunar year
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 ();}////// Obtain the calendar month of a calendar date //////One calendar date///
Lunar month
Public string GetMonth (DateTime time) {int month = calendar. getMonth (time); int year = calendar. getYear (time); int leap = 0; // The month lunar January cannot be a leap month for (int I = 3; I <= month; I ++) {if (calendar ar. 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];} ///// Obtain the calendar day of a calendar date //////One calendar date///
Lunar calendar day
Public string GetDay (DateTime time) {return ChineseDayName [calendar. GetDayOfMonth (time)-1] ;}}
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 (); // calendar date
The following is a test result:
Front-end code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestCountryDate._Default" %>
Background code:
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 ;}}
The effect is as follows:
The main time is the CountryDate class. you can call it to obtain the time. If there is better optimization, you are welcome to exchange.