Someone in the group of big guys who often chatted with me years ago wrote a form countdown to calculate the time to get off work and vacation time:)
It's simply mocking people like me who don't have a job, hahaha
The countdown in the form is quite untechnical, mainly because it is not cool enough and cannot reflect our identity as programmers.
So what does it mean to be cool? It must be a console!
The scenes of hackers crackling and scrolling in the TV series are so cool!
So, I studied how to time the time on the console.
Baidu came to the old post of msdn, and all the answers used threads. Then use threads.
The main timing method is to use the Thread.Sleep(1000); method to stop the main thread for one second.
Then loop the thread to call the parameter passing method Thread thread = new Thread(delegate() { GetTime(dt); });
This method calculates the time difference and then outputs the time.
In this way, a bug will be completed (be careful and create bugs with care). Although you calculate the time and output it, it basically won’t take much time.
But hundreds of times and thousands of times After this, it will cause one second to be lost in the two output times, but it does not matter to the final result.
(In hindsight, it should be possible to call the method without a thread, the main thing is 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; Change the color of the output characters is green.
Console.SetCursorPosition(8, 5); is a statement that changes the position of the cursor, which represents the 8th character of the 5th line of the cursor. At this time, the output will be output here.
The output result is still the original detailed time format, but it will be more beautiful with slight modifications. Here are some output statements about time.
获得当前系统时间: 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本身并不改变
The above is the detailed content of Detailed explanation of C# console countdown. For more information, please follow other related articles on the PHP Chinese website!