網站首頁 編程語言 正文
本文實例為大家分享了C#實現動態數字時鐘和日歷的具體代碼,供大家參考,具體內容如下
實現如下圖所示的簡易時鐘和日歷,要求顯示公歷日期、時間、星期、農歷日期。
首先新建一個ChineseCanlendar類用于實現和農歷相關的操作:
【ChineseCanlendar.cs】
/*
?* 作者:JeronZhou
?* 時間:2021-11-01
?* 功能:動態數字時鐘和日歷
?*/
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("年份無效!");
? ? ? ? }
? ? ? ? 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("月份無效!");
? ? ? ? }
? ? ? ? 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("無效日期!");
? ? ? ? }
? ? ? ? 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));
? ? ? ? }
? ? }
}
【窗體設計】
從上到下設置三個標簽,并添加背景顏色,為了使效果更佳,最好將窗口全部填滿:
【MainForm.cs】
/*
?* 作者:JeronZhou
?* 時間:2021-11-01
?* 功能:動態數字時鐘和日歷
?*/
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
?* 時間:2021-11-01
?* 功能:動態數字時鐘和日歷
?*/
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());
?? ??? ?}
?? ?}
}
【運行結果】
原文鏈接:https://blog.csdn.net/JeronZhou/article/details/121088302
相關推薦
- 2022-06-11 FreeRTOS進階之任務切換完全分析_操作系統
- 2022-11-20 Postgresql刪除數據庫表中重復數據的幾種方法詳解_PostgreSQL
- 2023-05-31 Pandas使用分隔符或正則表達式將字符串拆分為多列_python
- 2022-08-17 Android?Flutter表格組件Table的使用詳解_Android
- 2022-08-23 .net中的Span<T>類和Memory<T>類介紹_基礎應用
- 2022-06-10 ASP.NET?Core使用EF保存數據、級聯刪除和事務使用_實用技巧
- 2022-02-16 瀏覽器斷點如何使用(測試工具)
- 2021-12-13 C++??系統IO流介紹_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支