年前经常聊天的大佬群里有人写了窗体的倒计时来计算下班时间和放假时间:)

简直就是在嘲讽我这种没有工作的人,哈哈哈

窗体的倒计时相当的没有技术含量,主要是不够炫酷,不能够体现我们程序员的身份。

那什么才叫炫酷?必须是控制台啊!

电视剧上黑客噼里啪啦噼里啪啦滚屏的画面多炫酷!

所以,研究了一下怎么样在控制台上计时。

百度到了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 = ;
             //控制台高度25是最低
             Console.BufferHeight = ;
             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();
             }
             Console.ReadLine();
         }

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

Console.ForegroundColor = ConsoleColor.Green; 改变输出字符的颜色为绿色。

Console.SetCursorPosition(8, 5); 是改变光标的位置的语句,代表光标第5行第8个字符,这时候再进行输出就是在这里输出了。

这程序还是到处是BUG的,毕竟一天到晚写的是BUG,不是程序。

我的程序,使用的是.NET Framework 4.0,各位电脑上应该都有。

(密码:C2fyaK)

https://share.weiyun.com/076fa80d5a5b270c3f73b90e3fd77d48

输出的结果依然是原始详细的时间格式,不过稍作修改就会更美观一点,下面是一些有关时间的输出语句。

获得当前系统时间: 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());
Console.WriteLine(dt.Date.ToString());         // 26/11/2009 AM 12:00:00
Console.WriteLine(dt.DayOfWeek.ToString());     // Thursday
Console.WriteLine(dt.DayOfYear.ToString());
Console.WriteLine(dt.Hour.ToString());
Console.WriteLine(dt.Millisecond.ToString());     // 801 (毫秒)
Console.WriteLine(dt.Minute.ToString());
Console.WriteLine(dt.Month.ToString());
Console.WriteLine(dt.Second.ToString());     

Console.WriteLine(dt.TimeOfDay.ToString()); // 12:29:51.5181524

Console.WriteLine(dt.ToString()); // 26/11/2009 PM 12:29:51

Console.WriteLine(dt.AddYears().ToString()); // 26/11/2010 PM 12:29:51

Console.WriteLine(dt.CompareTo(dt).ToString()); 

Console.WriteLine(dt.Add(,,,)).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(].ToString()); //2009-11-26T13:29:06
Console.WriteLine(dt.GetDateTimeFormats(].ToString()); //PM 1:29
Console.WriteLine(dt.GetDateTimeFormats(].ToString()); //2009年11月
Console.WriteLine(dt.GetDateTimeFormats(].ToString()); //2009年11月26日
Console.WriteLine(dt.GetDateTimeFormats(].ToString()); //星期四, 26 十一月, 2009
Console.WriteLine(dt.GetDateTimeFormats(].ToString()); //26 十一月, 2009
Console.WriteLine(dt.GetDateTimeFormats(].ToString()); //星期四 2009 11 26
Console.WriteLine(dt.GetDateTimeFormats(].ToString()); //26 十一月
Console.WriteLine(dt.GetDateTimeFormats(].ToString()); //2009年11月26日 PM 1:29
Console.WriteLine(dt.GetDateTimeFormats(].ToString()); //26/11/2009 PM 1:29
Console.WriteLine(dt.GetDateTimeFormats(].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(

计算2个日期之间的天数差
DateTime dt1 = Convert.ToDateTime("2007-8-1");
DateTime dt2 = Convert.ToDateTime("2007-8-15");
TimeSpan span = dt2.Subtract(dt1);
int dayDiff = span.Days ;

计算某年某月的天数
, );
days = ;

给日期增加一天、减少一天
DateTime dt =DateTime.Now;
dt.AddDays(); //增加一天 dt本身并不改变
dt.AddDays(-);//减少一天 dt本身并不改变

转载请联系

C# 控制台倒计时的更多相关文章

  1. [WebShow系列] 倒计时展示相关问题

    WebShow内置了倒计时功能. 后台参数维护: 倒计时参数说明: 倒计时基准时间设置(秒数):假设设置为90,也就是从1分30秒开始倒计时,同时有开始提示音. 倒计时提示1剩余秒数:假设设置为30, ...

  2. 【系统移植】uboot详细分析

    uboot使用 uboot控制台,倒计时    命令: 调试,操作一些硬件 setenv printenv saveenv  nand erase  nand write  tftp zImage h ...

  3. 发个2012年用java写的一个控制台小游戏

    时间是把杀狗刀 突然发现了12年用java写的控制台玩的一个文字游戏,有兴趣的可以下载试试哈汪~ 里面难点当时确实遇到过,在计算倒计时的时候用了多线程,当时还写了好久才搞定.很怀念那个时间虽然不会做游 ...

  4. vue-实现倒计时功能

    JavaScript 创建一个 countdown 方法,用于计算并在控制台打印距目标时间的日.时.分.秒数,每隔一秒递归执行一次. msec 是当前时间距目标时间的毫秒数,由时间戳相减得到,我们将以 ...

  5. 【微信支付】分享一个失败的案例 跨域405(Method Not Allowed)问题 关于IM的一些思考与实践 基于WebSocketSharp 的IM 简单实现 【css3】旋转倒计时 【Html5】-- 塔台管制 H5情景意识 --飞机 谈谈转行

    [微信支付]分享一个失败的案例 2018-06-04 08:24 by stoneniqiu, 2744 阅读, 29 评论, 收藏, 编辑 这个项目是去年做的,开始客户还在推广,几个月后发现服务器已 ...

  6. js网页倒计时功能(天,时,分,秒)

    给定任何一个时间,然后实现现在到那个时间的倒计时. 下面的例子是显示现在到2019年8月1号0时0分的倒计时: <div class="list"> <span ...

  7. Flutter 快速上手定时器/倒计时及实战讲解

    本文微信公众号「AndroidTraveler」首发. 今天给大家讲讲 Flutter 里面定时器/倒计时的实现. 一般有两种场景: 我只需要你在指定时间结束后回调告诉我.回调只需要一次. 我需要你在 ...

  8. .NET平台开源项目速览(17)FluentConsole让你的控制台酷起来

    从该系列的第一篇文章 .NET平台开源项目速览(1)SharpConfig配置文件读写组件 开始,不知不觉已经到第17篇了.每一次我们都是介绍一个小巧甚至微不足道的.NET平台的开源软件,或者学习,或 ...

  9. .NET Core的日志[2]:将日志输出到控制台

    对于一个控制台应用,比如采用控制台应用作为宿主的ASP.NET Core应用,我们可以将记录的日志直接输出到控制台上.针对控制台的Logger是一个类型为ConsoleLogger的对象,Consol ...

随机推荐

  1. DataTable.DataRow的复制

    经常遇到这种错误,“此行已属于另一个表”的错误,导致这个错误的语句如下: dtPriceTable.Rows.InsertAt(aDataRow,i); 或者 dtPriceTable.Rows.Ad ...

  2. iOS workspace 依次编译多个工程

    目的:当我封装一个framework的时候,需要检验当前的改动,但是又不想编译完framework,又要编译测试工程. 步骤: 1. 选择测试工程 2. Edit Scheme 3. 选中Build- ...

  3. ZOJ 1011 - NTA

    题目大意:有一颗完全二叉树,给节点一个信号会从一个表中选择一对信号分别传递给两个子节点.最后判断所有叶子节点是否满足给定的规则.题目有点长,具体可参见原题. 首先是表格中数据的存储,由于会有多个元素, ...

  4. Mysql 创建数据库后修改属性

    添加表字段 alter table table1 add transactor varchar(10) not Null; alter table   table1 add id int unsign ...

  5. js原生之函数

    1.函数作为参数传给其他函数:    data.sort(function(a,b){return a-b})    //关于数组的sort函数,其回调函数返回负值,a在b之前    //正值,b在a ...

  6. Struts2文件的下载

    1.下载登录页面download.jsp 1: <%@ page language="java" contentType="text/html; charset=U ...

  7. 解剖 Elasticsearch 集群 - 之三

    解剖 Elasticsearch 集群 - 之三 本篇文章是一系列涵盖 Elasticsearch 底层架构和原型示例的其中一篇.在本篇文章中,我们会讨论 Elasticsearch 如何提供准实时搜 ...

  8. 用NetStream的appendBytes播放FLV

    public class MiniStream extends Sprite { private var _buffer:ByteArray = new ByteArray(); private va ...

  9. LINQ 的查询_联表、分组、排序

    1.查询 var v = from s in db.Set<ScoreInfo>().ToList()group s by s.subject into scoreselect new{  ...

  10. spring mvc 注解示例

    springmvc.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...