C# 控制台倒计时
年前经常聊天的大佬群里有人写了窗体的倒计时来计算下班时间和放假时间:)
简直就是在嘲讽我这种没有工作的人,哈哈哈
窗体的倒计时相当的没有技术含量,主要是不够炫酷,不能够体现我们程序员的身份。
那什么才叫炫酷?必须是控制台啊!
电视剧上黑客噼里啪啦噼里啪啦滚屏的画面多炫酷!
所以,研究了一下怎么样在控制台上计时。
百度到了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# 控制台倒计时的更多相关文章
- [WebShow系列] 倒计时展示相关问题
WebShow内置了倒计时功能. 后台参数维护: 倒计时参数说明: 倒计时基准时间设置(秒数):假设设置为90,也就是从1分30秒开始倒计时,同时有开始提示音. 倒计时提示1剩余秒数:假设设置为30, ...
- 【系统移植】uboot详细分析
uboot使用 uboot控制台,倒计时 命令: 调试,操作一些硬件 setenv printenv saveenv nand erase nand write tftp zImage h ...
- 发个2012年用java写的一个控制台小游戏
时间是把杀狗刀 突然发现了12年用java写的控制台玩的一个文字游戏,有兴趣的可以下载试试哈汪~ 里面难点当时确实遇到过,在计算倒计时的时候用了多线程,当时还写了好久才搞定.很怀念那个时间虽然不会做游 ...
- vue-实现倒计时功能
JavaScript 创建一个 countdown 方法,用于计算并在控制台打印距目标时间的日.时.分.秒数,每隔一秒递归执行一次. msec 是当前时间距目标时间的毫秒数,由时间戳相减得到,我们将以 ...
- 【微信支付】分享一个失败的案例 跨域405(Method Not Allowed)问题 关于IM的一些思考与实践 基于WebSocketSharp 的IM 简单实现 【css3】旋转倒计时 【Html5】-- 塔台管制 H5情景意识 --飞机 谈谈转行
[微信支付]分享一个失败的案例 2018-06-04 08:24 by stoneniqiu, 2744 阅读, 29 评论, 收藏, 编辑 这个项目是去年做的,开始客户还在推广,几个月后发现服务器已 ...
- js网页倒计时功能(天,时,分,秒)
给定任何一个时间,然后实现现在到那个时间的倒计时. 下面的例子是显示现在到2019年8月1号0时0分的倒计时: <div class="list"> <span ...
- Flutter 快速上手定时器/倒计时及实战讲解
本文微信公众号「AndroidTraveler」首发. 今天给大家讲讲 Flutter 里面定时器/倒计时的实现. 一般有两种场景: 我只需要你在指定时间结束后回调告诉我.回调只需要一次. 我需要你在 ...
- .NET平台开源项目速览(17)FluentConsole让你的控制台酷起来
从该系列的第一篇文章 .NET平台开源项目速览(1)SharpConfig配置文件读写组件 开始,不知不觉已经到第17篇了.每一次我们都是介绍一个小巧甚至微不足道的.NET平台的开源软件,或者学习,或 ...
- .NET Core的日志[2]:将日志输出到控制台
对于一个控制台应用,比如采用控制台应用作为宿主的ASP.NET Core应用,我们可以将记录的日志直接输出到控制台上.针对控制台的Logger是一个类型为ConsoleLogger的对象,Consol ...
随机推荐
- CSS实现背景透明而背景上的文字图片不透明
1.用图片则能兼容IE8和IE7 2.用颜色则不能兼容IE8和IE7,并且颜色层不能随着内容层自增长,只能设置一个固定高度 3.用颜色则颜色层不能包含内容层(图片和文字) <!-- wrap最外 ...
- Linux 下如何安装 JDK ,以 Ubuntu 为例。
http://www.cnblogs.com/memory4young/p/ubuntu-install-jdk.html 一.下载 首先,当然是要下载了. 地址:http://www.oracle. ...
- 使用 System.Transactions 进行事物管理
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- 动态创建Fastreport(delphi)
动态创建Fastreport分以下几个步骤: 1.首先清空Fastreport,定义全局变量,并加载数据集 frReport.Clear; frReport.DataSets.Add(fr ...
- 记录一次事故——idea,sbt,scala
没事千万不要点idea的update啊,就算它自己弹出来的也不要管哦. 我们自己的IDE在使用过程中总会有各种settting的配置,更新之后这些都没有了,而且自己本地安装的插件也就都没有了,所以更新 ...
- js原生设计模式——4安全的工厂方法模式之oop编程增强版
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- js blob
Blob 是什么? 这里说的是一种JavaScript的对象类型. Oracle 中也有类似的栏位类型. 在 [JS进阶] HTML5 之文件操作(file) 这一篇中用到了File对象,而实际上 f ...
- hadoop-1.x的运行实例
我的环境是hadoop-0.20.2,eclipse:SDK-3.3.2, 源数据为: Apr 23 11:49:54 hostapd: wlan0: STA 14:7d:c5:9e:fb:84 Ap ...
- xhtmlrenderer渲染pdf,中文换行
在实际开发中,发现在table中显示中文,渲染出来的pdf,中文内容不自动换行.经过搜索发现了一种解决方案,如下: 重写Breaker,修改right计算方式 /* * Breaker.java * ...
- 如何给js动态创建的dom添加事件
delegate() 方法 实例 当点击鼠标时,隐藏或显示 p 元素: $("div").delegate("button","click" ...