首先該計時軟件的功能是實現(xiàn)展示指定日期和當(dāng)天的日期差,比如2012年3月21日距離當(dāng)天已經(jīng)過去了多少天,2020年10月16日距離當(dāng)前還有多少天。
首先我們需要用到.net中的monthCalendar控件用來獲取指定日期。
代碼實現(xiàn):
??????????? DateTime startDate = monthCalendar1.SelectionStart;
//獲取指定日期
? ? ? ? ? ? DateTime currentDate = DateTime.Now;
//獲取當(dāng)前日期
? ? ? ? ? ? TimeSpan ts = currentDate - startDate;
//獲取時間差
? ? ? ? ? ? if (DateTime.Compare(startDate,currentDate) > 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? string[] labelText = ts.Days.ToString().Split(new char[] { '-' });
? ? ? ? ? ? ? ? label1.Text = labelText[1];
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? label1.Text = ts.Days.ToString()+"+";
? ? ? ? ? ? }
//判斷指定日期是在當(dāng)前日期的前還是后
功能實現(xiàn)后,只需要自己添加輸出代碼就可以了。
** 注意事項 **
日期是date而不是數(shù)據(jù)data,寫代碼時很容易寫錯,而且這個拼寫錯誤很難發(fā)現(xiàn)。