110,534
社区成员
发帖
与我相关
我的任务
分享
//我给你一个例子你参照自己改下
int times = 5;
private void timer1_Tick(object sender, EventArgs e)
{
times --;
textbox1.Text = "倒计时:" + times + "秒!";
if (times == 0)
{
timer1.Stop(); }
}
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;
}
}
partial class Form4
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.textBox1 = new System.Windows.Forms.TextBox();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(56, 26);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 21);
this.textBox1.TabIndex = 0;
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Form4
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 70);
this.Controls.Add(this.textBox1);
this.Name = "Form4";
this.Text = "Form4";
this.Load += new System.EventHandler(this.Form4_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Timer timer1;
}
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
this.textBox1.Text = DateTime.Now.ToString("mm:ss");
}
private void Form4_Load(object sender, EventArgs e)
{
timer1.Start();
}
}
<head>
<title>js倒计时</title>
<script type="text/javascript">
var total=2*3600;//总共多少秒
function showTime()
{
total--;
var hour=total/3600;
var sec=(total%3600)/60;
var min=(total%3600)%60;
document.getElementById("divId").innerHTML="还有"+hour+"小时"+sec+"分"+min+"秒";
if(total>0)
{
setTimeout("showTime()",1000);//每一秒钟执行一次检查
}
else
{
document.getElementById("divId").innerHTML="活动已结束!";
}
}
</script>
</head>
<body>
<div id="divId"></div>
</body>