溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C#如何實現(xiàn)動態(tài)數(shù)字時鐘和日歷

發(fā)布時間:2022-06-14 09:55:53 來源:億速云 閱讀:189 作者:iii 欄目:開發(fā)技術(shù)

這篇“C#如何實現(xiàn)動態(tài)數(shù)字時鐘和日歷”文章的知識點大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“C#如何實現(xiàn)動態(tài)數(shù)字時鐘和日歷”文章吧。

實現(xiàn)如下圖所示的簡易時鐘和日歷,要求顯示公歷日期、時間、星期、農(nóng)歷日期。

C#如何實現(xiàn)動態(tài)數(shù)字時鐘和日歷

首先新建一個ChineseCanlendar類用于實現(xiàn)和農(nóng)歷相關(guān)的操作:

【ChineseCanlendar.cs】

/*
 * 作者:JeronZhou
 * 時間:2021-11-01
 * 功能:動態(tài)數(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("年份無效!");
        }
        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));
        }
    }
}

【窗體設(shè)計】

從上到下設(shè)置三個標簽,并添加背景顏色,為了使效果更佳,最好將窗口全部填滿:

C#如何實現(xiàn)動態(tài)數(shù)字時鐘和日歷

【MainForm.cs】

/*
 * 作者:JeronZhou
 * 時間:2021-11-01
 * 功能:動態(tài)數(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
 * 時間:2021-11-01
 * 功能:動態(tài)數(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());
        }
    }
}

【運行結(jié)果】

C#如何實現(xiàn)動態(tài)數(shù)字時鐘和日歷

以上就是關(guān)于“C#如何實現(xiàn)動態(tài)數(shù)字時鐘和日歷”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI