博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WinForm(C#)倒计时(年月日时分秒)
阅读量:7053 次
发布时间:2019-06-28

本文共 2377 字,大约阅读时间需要 7 分钟。

一个朋友提到这个问题,就随手写了一个,本以为很容易,却写了差不多半个小时,关键是年月的判断,现把代码贴出来供需要的朋友参考,也希望能看到大家的计算方法,先在WinForm窗体上放一个label控件(ID为lblShow) 和一个Timer控件(ID为timerMain),后台代码如下:

    
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;
        }
    }

 

 效果图:

 

本文转自博客园博客,原文链接:,如需转载请自行联系原作者

你可能感兴趣的文章
jQuery判断checkbox是否选中的3种方法
查看>>
在sublime Text 3上编写并运行java程序
查看>>
LinkedList源码分析
查看>>
【算法介绍】哈希排序算法
查看>>
js数组操作(增、删、改、查)
查看>>
UpdatePanel and JQuery Plugin
查看>>
centos5.7下的kdump
查看>>
[JavaEE笔记]Cookie
查看>>
【HDOJ】1987 Decoding
查看>>
ELK+Filebeat (1)
查看>>
leetcode 443. String Compression
查看>>
在没联网环境下,启动tomcat出错
查看>>
关于Git bash-127.0.0.7:8888拒绝访问的小问题--环境变量
查看>>
Java EE(七)
查看>>
javascript变量声明提升(hoisting)
查看>>
有价值的数据
查看>>
LayUi超级好用的前端工具
查看>>
[Ubuntu] ubuntu的tty下挂载移动硬盘拷贝数据
查看>>
PowerBI分析个人Exchange邮箱数据
查看>>
犯了个低级错误
查看>>