C# winform 分秒倒计时器
小弟想制作一个mm:SS的倒计时器。从数据库中调出mm:ss格式的文本类型字段值,然后进行倒计时显示,请大虾指点。...
小弟想制作一个mm:SS的倒计时器。从数据库中调出mm:ss格式的文本类型字段值,然后进行倒计时显示,请大虾指点。
展开
2个回答
展开全部
public partial class FormTime : Form
{
public FormTime()
{
InitializeComponent();
}
private void FormTime_Load(object sender, EventArgs e)
{
timerMain.Enabled = true;
timerMain.Interval = 1000;
timerMain.Start();
}
private void timerMain_Tick(object sender, EventArgs e)
{
DateTime observeTime = DateTime.Parse("2023-11-22 22:45:30");//倒计时日期
DateTime now = DateTime.Now; //当前时间
TimeSpan ts = observeTime.Subtract(now); //两个时间之差
StringBuilder result=new StringBuilder(); //存放结果
int year=observeTime.Year - now.Year; //得到相差的年
int month = observeTime.Month - now.Month; //得到相差的月
int day = observeTime.Day - now.Day; //得到相差的日
int hmh=(observeTime.Hour-now.Hour)*3600+(observeTime.Minute-now.Minute)*60+(observeTime.Second-now.Second);
//如果时分秒比现在的时间晚
if(hmh<=0)
{
//向日借一
day--;
if((day<=0&&month>0)||(day<=0&&year>0))
{
//如果天数小于0向月借一
day=GetDay(now.Year, now.Month)- now.Day + observeTime.Day;
month--;
if(month<0&&year>0)
{
//如果月数小于0,向年借一,同时把月专为正数
month += 12;
year--;
}
}
}
//如果天数小于0向月借一
if ((day < 0 && month > 0) || (day < 0 && year > 0))
{
day = GetDay(now.Year, now.Month) - now.Day + observeTime.Day;
month--;
if (month < 0 && year > 0)
{
//如果月数小于0,向年借一,同时把月专为正数
month += 12;
year--;
}
}
//如果月数小于0,向年借一,同时把月专为正数
if (month < 0 && year > 0)
{
month += 12;
year--;
}
if (year<0||(year == 0 && month < 0)||(year == 0 && month==0&&day<0))
{
lblShow.Text = "已超过日期";
return;
}
if (year> 0)
{
result.Append(year+ "年");
}
if (month> 0)
{
result.Append(month+ "月");
}
if (day> 0)
{
result.Append(day+ "天");
}
if (ts.Hours > 0)
{
result.Append(ts.Hours + "时");
}
if (ts.Minutes > 0)
{
result.Append(ts.Minutes + "分");
}
if (ts.Seconds > 0)
{
result.Append(ts.Seconds + "秒");
}
if(result.Length==0)
{
result.Append("已超过日期");
}
lblShow.Text = result.ToString();
}
//输入月份后获得该月天数
private int GetDay(int year, int month)
{
int result = 31;
switch (month)
{
case 4:
case 6:
case 9:
case 11:
result = 30;
break;
case 2:
if ((year % 100 != 0) && (year% 4 == 0) || (year% 400 == 0))
{
result = 29;
}
else
{
result = 28;
}
break;
}
return result;
}
}
参考下,希望能帮到你。
{
public FormTime()
{
InitializeComponent();
}
private void FormTime_Load(object sender, EventArgs e)
{
timerMain.Enabled = true;
timerMain.Interval = 1000;
timerMain.Start();
}
private void timerMain_Tick(object sender, EventArgs e)
{
DateTime observeTime = DateTime.Parse("2023-11-22 22:45:30");//倒计时日期
DateTime now = DateTime.Now; //当前时间
TimeSpan ts = observeTime.Subtract(now); //两个时间之差
StringBuilder result=new StringBuilder(); //存放结果
int year=observeTime.Year - now.Year; //得到相差的年
int month = observeTime.Month - now.Month; //得到相差的月
int day = observeTime.Day - now.Day; //得到相差的日
int hmh=(observeTime.Hour-now.Hour)*3600+(observeTime.Minute-now.Minute)*60+(observeTime.Second-now.Second);
//如果时分秒比现在的时间晚
if(hmh<=0)
{
//向日借一
day--;
if((day<=0&&month>0)||(day<=0&&year>0))
{
//如果天数小于0向月借一
day=GetDay(now.Year, now.Month)- now.Day + observeTime.Day;
month--;
if(month<0&&year>0)
{
//如果月数小于0,向年借一,同时把月专为正数
month += 12;
year--;
}
}
}
//如果天数小于0向月借一
if ((day < 0 && month > 0) || (day < 0 && year > 0))
{
day = GetDay(now.Year, now.Month) - now.Day + observeTime.Day;
month--;
if (month < 0 && year > 0)
{
//如果月数小于0,向年借一,同时把月专为正数
month += 12;
year--;
}
}
//如果月数小于0,向年借一,同时把月专为正数
if (month < 0 && year > 0)
{
month += 12;
year--;
}
if (year<0||(year == 0 && month < 0)||(year == 0 && month==0&&day<0))
{
lblShow.Text = "已超过日期";
return;
}
if (year> 0)
{
result.Append(year+ "年");
}
if (month> 0)
{
result.Append(month+ "月");
}
if (day> 0)
{
result.Append(day+ "天");
}
if (ts.Hours > 0)
{
result.Append(ts.Hours + "时");
}
if (ts.Minutes > 0)
{
result.Append(ts.Minutes + "分");
}
if (ts.Seconds > 0)
{
result.Append(ts.Seconds + "秒");
}
if(result.Length==0)
{
result.Append("已超过日期");
}
lblShow.Text = result.ToString();
}
//输入月份后获得该月天数
private int GetDay(int year, int month)
{
int result = 31;
switch (month)
{
case 4:
case 6:
case 9:
case 11:
result = 30;
break;
case 2:
if ((year % 100 != 0) && (year% 4 == 0) || (year% 400 == 0))
{
result = 29;
}
else
{
result = 28;
}
break;
}
return result;
}
}
参考下,希望能帮到你。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询