本文主要是介绍C#精彩编程200例-第2例-使用Timer组件实现冬奥会倒计时,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
C#精彩编程200例-第2例-使用Timer组件实现冬奥会倒计时
- 1. 效果
- 2. 实现代码
1. 效果
2. 实现代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//要引用 Visual Basic 程序集
using Microsoft.VisualBasic;namespace Demo_2使用Timer组件实现冬奥会计时
{public partial class Frm_Timer : Form{public Frm_Timer(){InitializeComponent();}private void timer1_Tick(object sender, EventArgs e){//1.获取当前系统时间DateTime current_time = DateTime.Now;txb_CurrentTime.Text = current_time.ToString();//2.获取冬奥会开幕时间DateTime winter_time = Convert.ToDateTime(Convert.ToDateTime("2022-02-04 00:00:00"));txb_WInterOlympicsTime.Text = winter_time.ToString();//3.计算年、月、日、时、分、秒间隔时间txb_YearTime.Text = DateAndTime.DateDiff("yyyy", current_time, winter_time, FirstDayOfWeek.Sunday, FirstWeekOfYear.Jan1).ToString();txb_MonthTime.Text = DateAndTime.DateDiff("m", current_time, winter_time, FirstDayOfWeek.Sunday, FirstWeekOfYear.FirstFourDays).ToString();txb_DayTime.Text = DateAndTime.DateDiff("d", current_time, winter_time, FirstDayOfWeek.Sunday, FirstWeekOfYear.FirstFourDays).ToString();txb_HourTime.Text = DateAndTime.DateDiff("h", current_time, winter_time, FirstDayOfWeek.Sunday, FirstWeekOfYear.FirstFourDays).ToString();txb_MinuteTime.Text = DateAndTime.DateDiff("n", current_time, winter_time, FirstDayOfWeek.Sunday, FirstWeekOfYear.FirstFourDays).ToString();txb_SecondTime.Text = DateAndTime.DateDiff("s", current_time, winter_time, FirstDayOfWeek.Sunday, FirstWeekOfYear.FirstFourDays).ToString();}}
}
注意事项:
使用DateAndTime需要引用using Microsoft.VisualBasic;
这篇关于C#精彩编程200例-第2例-使用Timer组件实现冬奥会倒计时的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!