返回顶部
分享到

C#实现动态数字时钟和日历

编程 2022-8-12 08:54 386人浏览 0人回复
摘要

本文实例为大家分享了C#实现动态数字时钟和日历的具体代码,供大家参考,具体内容如下实现如下图所示的简易时钟和日历,要求显示公历日期、时间、星期、农历日期。首先新建一个ChineseCanlendar类用于实现和农历相关 ...

本文实例为大家分享了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());
}
}
}

【运行结果】


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

推广广告
星点云香港服务器,CN2高速连接,ping值低可免费换IP,安全稳定,技术团队24小时在线稳定无忧
本文暂无评论,快来抢沙发!

热门问答
“红客联盟” 源于黑客,是由一群热爱祖国、坚持正义、开拓进取的电脑爱好者组成。他们不利用网络技术入侵自己国家电脑,而是“维护正义,为国家争光的黑客群体”。我们的口号:“犯我中华者,虽远必诛”!
  • 官方公众号

  • 官方微博

  • 官方QQ群

  • 中国红客联盟 本站已运行|天天打卡 |
    | Copyright © 2009-2079 红客联盟版权所有| 粤ICP备13060014号
  • 本程序由 Tencent Cloud 提供开源 | Copyright © 2001-2020. |