C#實(shí)現(xiàn)動(dòng)態(tài)數(shù)字時(shí)鐘和日歷
本文實(shí)例為大家分享了C#實(shí)現(xiàn)動(dòng)態(tài)數(shù)字時(shí)鐘和日歷的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)如下圖所示的簡(jiǎn)易時(shí)鐘和日歷,要求顯示公歷日期、時(shí)間、星期、農(nóng)歷日期。
首先新建一個(gè)ChineseCanlendar類用于實(shí)現(xiàn)和農(nóng)歷相關(guān)的操作:
【ChineseCanlendar.cs】
/* ?* 作者:JeronZhou ?* 時(shí)間:2021-11-01 ?* 功能:動(dòng)態(tài)數(shù)字時(shí)鐘和日歷 ?*/ using System; using System.Linq; using System.Globalization; namespace Test2_1 { ? ? static public class ChineseCanlendar ? ? { ? ? ? ? private static ChineseLunisolarCalendar ChineseCalendar = new ChineseLunisolarCalendar(); ? ? ? ? private static string[] tg = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" }; ? ? ? ? private static string[] dz = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" }; ? ? ? ? private static string[] sx = { "鼠", "牛", "虎", "兔", "龍", "蛇", "馬", "羊", "猴", "雞", "狗", "豬" }; ? ? ? ? public static string GetLunisolarYear(int year) ? ? ? ? { ? ? ? ? ? ? if (year > 3) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? int tgIndex = (year - 4) % 10; ? ? ? ? ? ? ? ? int dzIndex = (year - 4) % 12; ? ? ? ? ? ? ? ? return string.Concat(tg[tgIndex], dz[dzIndex], "[", sx[dzIndex], "]"); ? ? ? ? ? ? } ? ? ? ? ? ? throw new ArgumentOutOfRangeException("年份無(wú)效!"); ? ? ? ? } ? ? ? ? private static string[] months = { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二(臘)" }; ? ? ? ? private static string[] days1 = { "初", "十", "廿", "三" }; ? ? ? ? private static string[] days = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" }; ? ? ? ? public static string GetLunisolarMonth(int month) ? ? ? ? { ? ? ? ? ? ? if (month < 13 && month > 0) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? return months[month - 1]; ? ? ? ? ? ? } ? ? ? ? ? ? throw new ArgumentOutOfRangeException("月份無(wú)效!"); ? ? ? ? } ? ? ? ? public static string GetLunisolarDay(int day) ? ? ? ? { ? ? ? ? ? ? if (day > 0 && day < 32) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? if (day != 20 && day != 30) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? return string.Concat(days1[(day - 1) / 10], days[(day - 1) % 10]); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? return string.Concat(days[(day - 1) / 10], days1[1]); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? throw new ArgumentOutOfRangeException("無(wú)效日期!"); ? ? ? ? } ? ? ? ? public static string GetChineseDateTime(DateTime datetime) ? ? ? ? { ? ? ? ? ? ? int year = ChineseCalendar.GetYear(datetime); ? ? ? ? ? ? int month = ChineseCalendar.GetMonth(datetime); ? ? ? ? ? ? int day = ChineseCalendar.GetDayOfMonth(datetime); ? ? ? ? ? ? int leapMonth = ChineseCalendar.GetLeapMonth(year); ? ? ? ? ? ? bool isleap = false; ? ? ? ? ? ? if (leapMonth > 0) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? if (leapMonth == month) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? isleap = true; ? ? ? ? ? ? ? ? ? ? month--; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else if (month > leapMonth) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? month--; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? return string.Concat(GetLunisolarYear(year), "年", isleap ? "閏" : string.Empty, GetLunisolarMonth(month), "月", GetLunisolarDay(day)); ? ? ? ? } ? ? } }
【窗體設(shè)計(jì)】
從上到下設(shè)置三個(gè)標(biāo)簽,并添加背景顏色,為了使效果更佳,最好將窗口全部填滿:
【MainForm.cs】
/* ?* 作者:JeronZhou ?* 時(shí)間:2021-11-01 ?* 功能:動(dòng)態(tài)數(shù)字時(shí)鐘和日歷 ?*/ using System; using System.Linq; using System.Windows.Forms; namespace Test2_1 { ?? ?public partial class MainForm : Form ? ? { ? ? ? ? public MainForm() ? ? ? ? { ? ? ? ? ? ? InitializeComponent(); ? ? ? ? ? ? timer1.Start(); ? ? ? ? } ?? ??? ?void Timer1Tick(object sender, EventArgs e) ?? ??? ?{ ?? ? ? ? ? ?label1.Text = DateTime.Now.ToString(); ?? ? ? ? ? ?switch(DateTime.Now.DayOfWeek.ToString()) ?? ? ? ? ? ?{ ?? ? ? ? ? ??? ?case "Monday": ?? ? ? ? ? ??? ??? ?label2.Text = "星期一"; break; ?? ? ? ? ? ??? ?case "Tuesday": ?? ? ? ? ? ??? ??? ?label2.Text = "星期二"; break; ?? ? ? ? ? ??? ?case "Wednesday": ?? ? ? ? ? ??? ??? ?label2.Text = "星期三"; break; ?? ? ? ? ? ??? ?case "Thursday": ?? ? ? ? ? ??? ??? ?label2.Text = "星期四"; break; ?? ? ? ? ? ??? ?case "Friday": ?? ? ? ? ? ??? ??? ?label2.Text = "星期五"; break; ?? ? ? ? ? ??? ?case "Saturday": ?? ? ? ? ? ??? ??? ?label2.Text = "星期六"; break; ?? ? ? ? ? ??? ?case "Sunday": ?? ? ? ? ? ??? ??? ?label2.Text = "星期日"; break; ?? ? ? ? ? ?} ? ? ? ? ? ? string GCDT = ChineseCanlendar.GetChineseDateTime(DateTime.Now); ? ? ? ? ? ? label3.Text = GCDT; ?? ??? ?} ? ? } }
【Program.cs】
/* ?* 作者:JeronZhou ?* 時(shí)間:2021-11-01 ?* 功能:動(dòng)態(tài)數(shù)字時(shí)鐘和日歷 ?*/ using System; using System.Windows.Forms; namespace Test2_1 { ?? ?internal sealed class Program ?? ?{ ?? ??? ?[STAThread] ?? ??? ?private static void Main(string[] args) ?? ??? ?{ ?? ??? ??? ?Application.EnableVisualStyles(); ?? ??? ??? ?Application.SetCompatibleTextRenderingDefault(false); ?? ??? ??? ?Application.Run(new MainForm()); ?? ??? ?} ?? ?} }
【運(yùn)行結(jié)果】
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#數(shù)據(jù)綁定(DataBinding)簡(jiǎn)單實(shí)現(xiàn)方法
這篇文章主要介紹了C#數(shù)據(jù)綁定(DataBinding)簡(jiǎn)單實(shí)現(xiàn)方法,以簡(jiǎn)單實(shí)例形式簡(jiǎn)單分析了C#實(shí)現(xiàn)數(shù)據(jù)綁定與讀取的方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08C#難點(diǎn)逐個(gè)擊破(9):類型轉(zhuǎn)換
類型之間的轉(zhuǎn)換可以分為隱式轉(zhuǎn)換與顯式轉(zhuǎn)換,如int類型可直接轉(zhuǎn)換為long類型。2010-02-02C#代碼設(shè)置開(kāi)機(jī)啟動(dòng)示例
本文介紹了使用C#代碼設(shè)置開(kāi)機(jī)啟動(dòng)的方法,原理就是在注冊(cè)表啟動(dòng)項(xiàng)里添加一項(xiàng)2014-01-01c# 將Minio.exe注冊(cè)成windows服務(wù)
這篇文章主要介紹了c# 如何將Minio.exe注冊(cè)成windows服務(wù),幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2020-11-11C# Winform下載文件并顯示進(jìn)度條的實(shí)現(xiàn)代碼
本來(lái)是要研究怎樣判斷下載完成,結(jié)果找到這個(gè)方法,可以在這個(gè)方法完成之后提示下載完成,需要的朋友可以參考下2014-07-07C#實(shí)現(xiàn)為類和函數(shù)代碼自動(dòng)添加版權(quán)注釋信息的方法
這篇文章主要介紹了C#實(shí)現(xiàn)為類和函數(shù)代碼自動(dòng)添加版權(quán)注釋信息的方法,主要涉及安裝文件的修改及函數(shù)注釋模板的修改,需要的朋友可以參考下2014-09-09