年前經常聊天的大佬群里有人寫了窗體的倒計時來計算下班時間和放假時間:) 簡直就是在嘲諷我這種沒有工作的人,哈哈哈 窗體的倒計時相當的沒有技術含量,主要是不夠炫酷,不能夠體現我們程式員的身份。 那什麼才叫炫酷?必須是控制台啊! 電視劇上黑客噼里啪啦噼里啪啦滾屏的畫面多炫酷! 所以,研究了一下怎麼樣在控 ...
年前經常聊天的大佬群里有人寫了窗體的倒計時來計算下班時間和放假時間:)
簡直就是在嘲諷我這種沒有工作的人,哈哈哈
窗體的倒計時相當的沒有技術含量,主要是不夠炫酷,不能夠體現我們程式員的身份。
那什麼才叫炫酷?必須是控制台啊!
電視劇上黑客噼里啪啦噼里啪啦滾屏的畫面多炫酷!
所以,研究了一下怎麼樣在控制臺上計時。
百度到了msdn的老帖子,所有的回答都是使用線程。那就用線程吧。
主要的計時方法就是使用 Thread.Sleep(1000); 方法,使主線程停止一秒。
然後迴圈使用線程調用傳參方法 Thread thread = new Thread(delegate() { GetTime(dt); });
這個方法計算時間差然後輸出時間。
這樣就會完成一個bug(比心,用心創造BUG),雖然計算一下時間,再輸出,基本上是用不了多少時間的,
但是幾百次幾千次以後就會導致兩次輸出時間里少了一秒,但對於最後的結果來說並無大礙。
(後知後覺,不用線程調用方法應該也可以,主要還是Thread.Sleep(1000);)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading; 6 7 namespace Timer 8 { 9 class Program 10 { 11 static bool isEnd = false; 12 13 static void Main(string[] args) 14 { 15 //控制台標題 16 Console.Title = "Timer"; 17 //控制台寬度 18 Console.WindowWidth = 50; 19 //控制台高度25是最低 20 Console.BufferHeight = 25; 21 Console.WriteLine("現在的時間是"); 22 Console.WriteLine(DateTime.Now); 23 Console.WriteLine("輸入截至時間"); 24 25 //獲取設定時間 26 DateTime dt = Convert.ToDateTime(Console.ReadLine()); 27 //獲取開始時間 28 DateTime dtNow = Convert.ToDateTime(DateTime.Now); 29 30 31 //調用線程獲取時間 32 while (isEnd == false) 33 { 34 Thread thread = new Thread(delegate() { GetTime(dt); }); 35 thread.Start(); 36 Thread.Sleep(1000); 37 } 38 Console.ReadLine(); 39 } 40 41 42 43 //輸出計時剩餘時間 44 static void GetTime(DateTime dt) 45 { 46 //獲取開始時間 47 DateTime dtNow = Convert.ToDateTime(DateTime.Now); 48 TimeSpan ts = dt - dtNow; 49 Console.ForegroundColor = ConsoleColor.Green; 50 Console.SetCursorPosition(8, 5); 51 Console.WriteLine(ts); 52 if (ts.TotalSeconds <= 1) 53 { 54 Console.SetCursorPosition(0, 9); 55 Console.WriteLine("計時已完成"); 56 isEnd = true; 57 } 58 } 59 } 60 }
Console.ForegroundColor = ConsoleColor.Green; 改變輸出字元的顏色為綠色。
Console.SetCursorPosition(8, 5); 是改變游標的位置的語句,代表游標第5行第8個字元,這時候再進行輸出就是在這裡輸出了。
這程式還是到處是BUG的,畢竟一天到晚寫的是BUG,不是程式。
我的程式,使用的是.NET Framework 4.0,各位電腦上應該都有。
(密碼:C2fyaK)
https://share.weiyun.com/076fa80d5a5b270c3f73b90e3fd77d48
輸出的結果依然是原始詳細的時間格式,不過稍作修改就會更美觀一點,下麵是一些有關時間的輸出語句。
![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
獲得當前系統時間: DateTime dt = DateTime.Now; Environment.TickCount可以得到“系統啟動到現在”的毫秒值 DateTime now = DateTime.Now; Console.WriteLine(now.ToString("yyyy-MM-dd")); //按yyyy-MM-dd格式輸出s Console.WriteLine(dt.ToString()); // 26/11/2009 AM 11:21:30 Console.WriteLine(dt.Year.ToString()); // 2009 Console.WriteLine(dt.Date.ToString()); // 26/11/2009 AM 12:00:00 Console.WriteLine(dt.DayOfWeek.ToString()); // Thursday Console.WriteLine(dt.DayOfYear.ToString()); // 330 Console.WriteLine(dt.Hour.ToString()); // 11 Console.WriteLine(dt.Millisecond.ToString()); // 801 (毫秒) Console.WriteLine(dt.Minute.ToString()); // 21 Console.WriteLine(dt.Month.ToString()); // 11 Console.WriteLine(dt.Second.ToString()); // 30 Console.WriteLine(dt.TimeOfDay.ToString()); // 12:29:51.5181524 Console.WriteLine(dt.ToString()); // 26/11/2009 PM 12:29:51 Console.WriteLine(dt.AddYears(1).ToString()); // 26/11/2010 PM 12:29:51 Console.WriteLine(dt.CompareTo(dt).ToString()); // 0 Console.WriteLine(dt.Add(new TimeSpan(1,0,0,0)).ToString()); // 加上一個時間段 (註: System.TimeSpan為一個時間段,構造函數如下 public TimeSpan(long ticks); // ticks: A time period expressed in 100-nanosecond units. //nanosecond:十億分之一秒 new TimeSpan(10,000,000) 為一秒 public TimeSpan(int hours, int minutes, int seconds); public TimeSpan(int days, int hours, int minutes, int seconds); public TimeSpan(int days, int hours, int minutes, int seconds, int milliseconds); ) Console.WriteLine("The Time is {0}",End-Start); Console.WriteLine(dt.GetDateTimeFormats('s')[0].ToString()); //2009-11-26T13:29:06 Console.WriteLine(dt.GetDateTimeFormats('t')[0].ToString()); //PM 1:29 Console.WriteLine(dt.GetDateTimeFormats('y')[0].ToString()); //2009年11月 Console.WriteLine(dt.GetDateTimeFormats('D')[0].ToString()); //2009年11月26日 Console.WriteLine(dt.GetDateTimeFormats('D')[1].ToString()); //星期四, 26 十一月, 2009 Console.WriteLine(dt.GetDateTimeFormats('D')[2].ToString()); //26 十一月, 2009 Console.WriteLine(dt.GetDateTimeFormats('D')[3].ToString()); //星期四 2009 11 26 Console.WriteLine(dt.GetDateTimeFormats('M')[0].ToString()); //26 十一月 Console.WriteLine(dt.GetDateTimeFormats('f')[0].ToString()); //2009年11月26日 PM 1:29 Console.WriteLine(dt.GetDateTimeFormats('g')[0].ToString()); //26/11/2009 PM 1:29 Console.WriteLine(dt.GetDateTimeFormats('r')[0].ToString()); //Thu, 26 Nov 2009 13:29:06 GMT (註: 常用的日期時間格式: 格式 說明 輸出格式 d 精簡日期格式 MM/dd/yyyy D 詳細日期格式 dddd, MMMM dd, yyyy f 完整格式 (long date + short time) dddd, MMMM dd, yyyy HH:mm F 完整日期時間格式 (long date + long time) dddd, MMMM dd, yyyy HH:mm:ss g 一般格式 (short date + short time) MM/dd/yyyy HH:mm G 一般格式 (short date + long time) MM/dd/yyyy HH:mm:ss m,M 月日格式 MMMM dd s 適中日期時間格式 yyyy-MM-dd HH:mm:ss t 精簡時間格式 HH:mm T 詳細時間格式 HH:mm:ss ) Console.WriteLine(string.Format("{0:d}", dt)); //28/12/2009 Console.WriteLine(string.Format("{0:D}", dt)); //2009年12月28日 Console.WriteLine(string.Format("{0:f}", dt)); //2009年12月28日 AM 10:29 Console.WriteLine(string.Format("{0:F}", dt)); //2009年12月28日 AM 10:29:18 Console.WriteLine(string.Format("{0:g}", dt)); //28/12/2009 AM 10:29 Console.WriteLine(string.Format("{0:G}", dt)); //28/12/2009 AM 10:29:18 Console.WriteLine(string.Format("{0:M}", dt)); //28 十二月 Console.WriteLine(string.Format("{0:R}", dt)); //Mon, 28 Dec 2009 10:29:18 GMT Console.WriteLine(string.Format("{0:s}", dt)); //2009-12-28T10:29:18 Console.WriteLine(string.Format("{0:t}", dt)); //AM 10:29 Console.WriteLine(string.Format("{0:T}", dt)); //AM 10:29:18 Console.WriteLine(string.Format("{0:u}", dt)); //2009-12-28 10:29:18Z Console.WriteLine(string.Format("{0:U}", dt)); //2009年12月28日 AM 2:29:18 Console.WriteLine(string.Format("{0:Y}", dt)); //2009年12月 Console.WriteLine(string.Format("{0:yyyyMMddHHmmssffff}", dt)); //200912281029182047 計算2個日期之間的天數差 DateTime dt1 = Convert.ToDateTime("2007-8-1"); DateTime dt2 = Convert.ToDateTime("2007-8-15"); TimeSpan span = dt2.Subtract(dt1); int dayDiff = span.Days ; 計算某年某月的天數 int days = DateTime.DaysInMonth(2009, 8); days = 31; 給日期增加一天、減少一天 DateTime dt =DateTime.Now; dt.AddDays(1); //增加一天 dt本身並不改變 dt.AddDays(-1);//減少一天 dt本身並不改變View Code
轉載請聯繫