添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
using System.Threading.Tasks; using System.Windows.Forms; using Timer = System.Threading.Timer; //引用 namespace TimeRefresh public partial class Form1 : Form private Timer _tTimeUI; public Form1 () InitializeComponent(); _tTimeUI = new Timer(obj => TimeUI(), null , 1 * 1000 , 1 * 1000 ); public void TimeUI () DateTime nowDatetime = DateTime.Now; lbl_date.Text = GetCurrentYMD(nowDatetime); lbl_time.Text = $" {GetCurrentHMS(nowDatetime)} {GetCurrentWeekDay(nowDatetime)} " ; /// <summary> /// 获取时分秒, /// 默认是24小时制,如果hourFormat=12就是12小时,输入其他数依然是24小时 /// </summary> /// <returns> 时分秒字符串 </returns> public string GetCurrentHMS ( DateTime CurrentDateTime, int hourFormat = 24 , bool isRefreshTime = false ) if (hourFormat == 12 ) return CurrentDateTime.ToString( "hh:mm:ss" ); return CurrentDateTime.ToString( "HH:mm:ss" ); /// <summary> /// 获取年月日 /// </summary> /// <returns> 年月日字符串 </returns> public string GetCurrentYMD ( DateTime CurrentDateTime, bool isRefreshTime = false ) return CurrentDateTime.ToString( "yyyy年MM月dd日" ); /// <summary> /// 获取日期 /// </summary> public string GetCurrentWeekDay ( DateTime CurrentDateTime, bool isRefreshTime = false ) return CaculateWeekDay(CurrentDateTime.Year, CurrentDateTime.Month, CurrentDateTime.Day); /// <summary> /// 基姆拉尔森计算公式计算日期 /// </summary> /// <param name="y"> </param> /// <param name="m"> </param> /// <param name="d"> </param> /// <returns> 星期几 </returns> protected string CaculateWeekDay ( int y, int m, int d ) if (m == 1 || m == 2 ) m += 12 ; y--; //把一月和二月看成是上一年的十三月和十四月,例:如果是2004-1-10则换算成:2003-13-10来代入公式计算。 var week = (d + 2 * m + 3 * (m + 1 ) / 5 + y + y / 4 - y / 100 + y / 400 ) % 7 ; var weekstr = "" ; switch (week) case 0 : weekstr = "星期一" ; break ; case 1 : weekstr = "星期二" ; break ; case 2 : weekstr = "星期三" ; break ; case 3 : weekstr = "星期四" ; break ; case 4 : weekstr = "星期五" ; break ; case 5 : weekstr = "星期六" ; break ; case 6 : weekstr = "星期日" ; break ; return weekstr;

运行效果如下:

欢迎进qq群交流:704028989