ASP. NET displays the lunar calendar time (2), and asp.net displays the lunar calendar time

Source: Internet
Author: User

ASP. NET displays the lunar calendar time (2), and asp.net displays the lunar calendar time

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 ", "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", "2", "3", "4 ", "5", "6", "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 of a calendar month. /// </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 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 S TringBuilder (); 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 lunar 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; // The leap month cannot be leap 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];} /// <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

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" %> <! 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) {} 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.

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.