zoukankan      html  css  js  c++  java
  • C#控制台-计算当前时间占比和获取时间

    大家在使用C#进行编程时,总会遇到,一些实时显示刷新时间的项目,但是呢,许多刚接触C#的小白简直是噩梦,尤其使用WPF时,又没有Timer控件,好不容易找到了显示时间代码,发现只能显示一次最新读取的时间,这里演示控制台程序窗体或者wpf应用会在接下来的文章中讲述:

    在此演示的时间操作与html的时间逻辑相似,个人主页上的时间以及时间百分比都是通过这个C#程序进行2次开发和编程的,html版的获取时间将会写在博客专题中,择日再进行搬移.

    创建一个c#控制台程序

    获取本地时间的代码如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                System.Timers.Timer t = new System.Timers.Timer();
                t.Interval = 1000;    //刷新时间
                t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
                t.Start();
                Console.ReadKey();
            }
            static void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {
                Console.Write("");    //代表退格符,与u0008匹配
                Console.Write(DateTime.Now.ToString("yyyy-dd-MM HH:mm:ss"));  //y表示年,d表示月,m表示日,h表示小时,m表示时间,s表示秒,在html中也可以使用这个格式.
            }
        }
    }
    

    c# 时间百分比

    上次在某个手机app上看到有当前时间的占比情况,这个是个很好的idea,但可惜那个app上的算法是错的(偷笑),它的算法是:

    当前时间 ÷ 一天24小时 = 当前时间的占比 

     但当到了凌晨0点-凌晨1点的时候,速度是不正常且有偏差的所以经过多次和一位数学大佬商讨演算后,公式如下

     (((小时*3600)+ (分钟*60) + (秒))/864)

    话不多说直接上源码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace timenow
    {
        class Program
        {
            /// 判断当前时间过了今天的百分之多少
            /// 
            /// 
            static double DayTimePassed()
            {
                DateTime dt = DateTime.Now;
                double Numerator = dt.Hour * 60 * 60 + dt.Minute * 60 + dt.Second;
                double Denominator = 24 * 60 * 60;
                return Numerator / Denominator;
            }
            /// 
            /// 判断指定时间过了当日的百分之多少
            /// 
            /// 
            /// 
            static double DayTimePassed(DateTime dt)
            {
                double Numerator = dt.Hour * 60 * 60 + dt.Minute * 60 + dt.Second;
                double Denominator = 24 * 60 * 60;
                return Numerator / Denominator;
            }
            static void Main(string[] args)
            {
                System.Timers.Timer t = new System.Timers.Timer();
                t.Interval = 1000;
                t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
                t.Start();
                Console.ReadKey();
    
    
            }
            static void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {
                Console.Clear();//刷新屏幕
                Console.WriteLine(DateTime.Now.ToString());
                Console.WriteLine("Day: " + DayTimePassed().ToString("P"));
                Console.WriteLine("Day: " + DayTimePassed(DateTime.Now).ToString("P"));
                Console.ReadLine();
    
            }
    
        }
    }

    两种方式有共通之处,在使用中取决于是否需要调用内部数据或是直接输出时间.(也希望那家app厂商进行改进吧 (‾◡◝))

  • 相关阅读:
    学习笔记 四边形不等式的模型
    题解 AT2230【Water Distribution】
    题解 CF848C 【Goodbye Souvenir】
    第二届全国大学生算法设计与编程挑战赛(春季赛)正式赛题解&比赛回顾
    积性函数相关学习笔记
    【MySQL】数据库备份
    【SpringMVC】SSM的maven依赖
    【SpringMVC】Jackson解决乱码问题配置
    【SpringMVC】DispatcherServlet与中文过滤器配置
    【Spring】学习笔记008--AOP实现方式
  • 原文地址:https://www.cnblogs.com/torwen/p/13125648.html
Copyright © 2011-2022 走看看