首頁  >  文章  >  後端開發  >  詳解C# 控制台倒數計時

詳解C# 控制台倒數計時

迷茫
迷茫原創
2017-03-26 14:56:564202瀏覽

年前常聊天的大佬群組裡有人寫了窗體的倒數計時來計算下班時間和放假時間:)

簡直就是在嘲諷我這種沒有工作的人,哈哈哈

窗體的倒數計時相當的沒有技術含量,主要是不夠酷,不能夠體現我們程式設計師的身份。

那什麼才叫酷?必須是控制台啊!

電視劇上黑客噼裡啪啦噼裡啪啦滾屏的畫面多酷炫!

所以,研究了一下怎麼樣在控制台上計時。

百度到了msdn的老帖子,所有的回答都是使用線程。那就用線程吧。

主要的計時方法就是使用 Thread.Sleep(1000); 方法,讓主執行緒停止一秒。

然後循環使用執行緒呼叫傳參方法 Thread thread = new Thread(delegate() { GetTime(dt); });

這個方法計算時間差然後輸出時間。

這樣就會完成一個bug(比心,用心創造BUG),雖然計算一下時間,再輸出,基本上是用不了多少時間的,

但是幾百次幾千次以後就會導致兩次輸出時間裡少了一秒,但對於最後的結果來說並無大礙。

(後知後覺,不用線程呼叫方法應該也可以,主要還是Thread.Sleep(1000);)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Timer
{
    class Program
    {
        static bool isEnd = false;

        static void Main(string[] args)
        {
            //控制台标题
            Console.Title = "Timer";
            //控制台宽度
            Console.WindowWidth = 50;
            //控制台高度25是最低
            Console.BufferHeight = 25;
            Console.WriteLine("现在的时间是");
            Console.WriteLine(DateTime.Now);
            Console.WriteLine("输入截至时间");

            //获取设定时间
            DateTime dt = Convert.ToDateTime(Console.ReadLine());
            //获取开始时间
            DateTime dtNow = Convert.ToDateTime(DateTime.Now);


            //调用线程获取时间
            while (isEnd == false)
            {
                Thread thread = new Thread(delegate() { GetTime(dt); });
                thread.Start();
                Thread.Sleep(1000);
            }
            Console.ReadLine();
        }



        //输出计时剩余时间
        static void GetTime(DateTime dt)
        {
            //获取开始时间
            DateTime dtNow = Convert.ToDateTime(DateTime.Now);
            TimeSpan ts = dt - dtNow;
            Console.ForegroundColor = ConsoleColor.Green;
            Console.SetCursorPosition(8, 5);
            Console.WriteLine(ts);
            if (ts.TotalSeconds <= 1)
            {
                Console.SetCursorPosition(0, 9);
                Console.WriteLine("计时已完成");
                isEnd = true;
            }
        }
    }
}

Console.ForegroundColor = ConsoleColor.Green; 改變輸出字元的顏色為綠色。

Console.SetCursorPosition(8, 5); 是改變遊標的位置的語句,代表遊標第5行第8個字符,這時候再進行輸出就是在這裡輸出了。

輸出的結果依然是原始詳細的時間格式,不過稍作修改就會更美觀一點,以下是一些有關時間的輸出語句。

获得当前系统时间: DateTime dt = DateTime.Now;

Environment.TickCount可以得到“系统启动到现在”的毫秒值

DateTime now = DateTime.Now;

Console.WriteLine(now.ToString("yyyy-MM-dd"));     //按yyyy-MM-dd格式输出sConsole.WriteLine(dt.ToString());     // 26/11/2009 AM 11:21:30Console.WriteLine(dt.Year.ToString());         // 2009Console.WriteLine(dt.Date.ToString());         // 26/11/2009 AM 12:00:00Console.WriteLine(dt.DayOfWeek.ToString());     // ThursdayConsole.WriteLine(dt.DayOfYear.ToString());     // 330Console.WriteLine(dt.Hour.ToString());         // 11Console.WriteLine(dt.Millisecond.ToString());     // 801 (毫秒)Console.WriteLine(dt.Minute.ToString());     // 21Console.WriteLine(dt.Month.ToString());     // 11Console.WriteLine(dt.Second.ToString());     // 30Console.WriteLine(dt.TimeOfDay.ToString()); // 12:29:51.5181524Console.WriteLine(dt.ToString()); // 26/11/2009 PM 12:29:51Console.WriteLine(dt.AddYears(1).ToString()); // 26/11/2010 PM 12:29:51Console.WriteLine(dt.CompareTo(dt).ToString()); // 0Console.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:06Console.WriteLine(dt.GetDateTimeFormats('t')[0].ToString()); //PM 1:29Console.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 十一月, 2009Console.WriteLine(dt.GetDateTimeFormats('D')[2].ToString()); //26 十一月, 2009Console.WriteLine(dt.GetDateTimeFormats('D')[3].ToString()); //星期四 2009 11 26Console.WriteLine(dt.GetDateTimeFormats('M')[0].ToString()); //26 十一月Console.WriteLine(dt.GetDateTimeFormats('f')[0].ToString()); //2009年11月26日 PM 1:29Console.WriteLine(dt.GetDateTimeFormats('g')[0].ToString()); //26/11/2009 PM 1:29Console.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/2009Console.WriteLine(string.Format("{0:D}", dt)); //2009年12月28日Console.WriteLine(string.Format("{0:f}", dt)); //2009年12月28日 AM 10:29Console.WriteLine(string.Format("{0:F}", dt)); //2009年12月28日 AM 10:29:18Console.WriteLine(string.Format("{0:g}", dt)); //28/12/2009 AM 10:29Console.WriteLine(string.Format("{0:G}", dt)); //28/12/2009 AM 10:29:18Console.WriteLine(string.Format("{0:M}", dt)); //28 十二月Console.WriteLine(string.Format("{0:R}", dt)); //Mon, 28 Dec 2009 10:29:18 GMTConsole.WriteLine(string.Format("{0:s}", dt)); //2009-12-28T10:29:18Console.WriteLine(string.Format("{0:t}", dt)); //AM 10:29Console.WriteLine(string.Format("{0:T}", dt)); //AM 10:29:18Console.WriteLine(string.Format("{0:u}", dt)); //2009-12-28 10:29:18ZConsole.WriteLine(string.Format("{0:U}", dt)); //2009年12月28日 AM 2:29:18Console.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本身并不改变

以上是詳解C# 控制台倒數計時的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn