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 ...
随机推荐
- GP项目总结(一)
1.使用activity渲染不同的View时,两种方法: (1.)自定义两个不同的View,然后在mainActivity里根据不同的数据使用不同的View,通过addView()来Activity里 ...
- 【Xilinx-Petalinux学习】-04-OpenCV的移植
交叉编译PC平台 VMware12, CentOS 6.5 32 bit 在VMware中安装CentOS,用户名:xilinx-arm-opencv 密码:root 至于这里为什么用CentOS,而 ...
- [Usaco2008 Dec]Secret Message 秘密信息
2794: [Usaco2008 Dec]Secret Message 秘密信息 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 7 Solved: 3 ...
- Leetcode 175. Combine Two Tables
Table: Person +-------------+---------+ | Column Name | Type | +-------------+---------+ | PersonId ...
- 如何设置打开jsp页面速度加快?
1.
- eclipse 工具栏修改
本来和同学约好一起去吃饭的,刚电话说有亲戚过来了,叫我一起去吃 哪有那个闲心,去陪他们吃饭 刚好,把这个一起写了 相信很多人会很烦,eclipse的工具栏太多了,折了一行下来,看着不好看,还烦(本人觉 ...
- --@ui-router——$state服务原版详解
$state service in module ui.router.state Description $state service is responsible for representing ...
- Delphi实例之一个较复杂的记事本的实现
http://www.mamicode.com/info-detail-110813.html delphi中控件位置及自动排版的问题 http://blog.csdn.net/avan_lau/ar ...
- Android中支持的距离单位
px(像素):每个px对应屏幕上的一个点 dip或dp(device independent pixels,设备独立像素):一种基于屏幕密度的抽象单位.在每英寸160点的显示器上,1dip=1px.但 ...
- C# winform DatagridView 的简单操作
数据显示操作: dgBill.Columns[0].DataPropertyName = "key1"; dgBill.Columns[1].DataPropertyName = ...