zoukankan      html  css  js  c++  java
  • C#中的动态时间显示

    在平时的编程当中,有时为了规定一个时间段,或者计算你做某件事情所花费的时间,也或者要显示当前的时间等,我们要对时间做出相应的处理:倒计时和当前时间的显示。

    那么怎么样才可以实现这些时间的显示功能呢?现在就倒计时和当前时间的显示的实现简单的介绍一下:

    倒计时:

         首先我们要计算出所花时间当中用到所有秒数,通过这些秒数得出和小时,分钟之间的关系,然后通过time事件和String.format()格式化字符串将事件显示出来就可以了,下面我们来看一个例子

    假如要编写一个学生考试管理系统,考试的事件规定为20分钟,随着考试的进行时间也在不断的减少,那么就需要设计一个倒计时器:

    首先我们可以得到考试的事件为20分钟 也就是1200秒,所以可以得到

    1200秒中有0小时 20分钟 0秒

    现在我们来定义这三个变量:小时 分钟 秒

      int hour = t / 3600;

                     int minute = (t / 60)%60;

       int second = t % 60;

    上面得到了他们之间的关系,随着秒数的变化,小时和分钟也在相应的发生变化

     

    其次来看看时间显示的格式:

    String time=string.format(“{0:00}:{1:00}:{2:00}”,hour,minute,second)

    注:string.Format可以用来格式化字符串

    上面的格式时为了显示时间的时候可以显示00:00:00的效果,假如你不时按照上面写的那样

    String time=string.format(“{0}:{1}:{2}”,hour,minute,second)

    显示的效果就时0:0:0很不好

    最后根据time事件 当秒数大于0的时候不段减1就可以了总的实现效果如下面所示:

                 int t=1200;

                 private void timer1_Tick(object sender, EventArgs e)

                 {

                  if(t>0)

                   {

                    t--;

                    int h = t / 3600;

                    int m = (t / 60)%60;

                    int s = t % 60;

                    string time = String.Format("{0:00}:{1:00}:{2:00}",h,m,s);

    }

    如果是b/s程序即asp.net,就要利用Ajax技术了,使用timer控件,就按照下面的写

    <asp:ScriptManager ID="ScriptManager1" runat="server">

            </asp:ScriptManager>

            <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">

            <ContentTemplate>

                <asp:Label ID="Label1" runat="server" ></asp:Label>

                <asp:Timer ID="Timer1" runat="server" ontick="Timer1_Tick" Interval="1000">

            </asp:Timer>

            </ContentTemplate>

            </asp:UpdatePanel>

    后台代码:

       public int t

        {

            get { return Convert.ToInt32(ViewState["time"]); }

            set { ViewState["time"]= value; }

        }

       

      protected void Page_Load(object sender, EventArgs e)

        {

            if(!IsPostBack)

            {

                t = 1200;

                this.Label1.Text = t.ToString();

            }

        }

     

        protected void Timer1_Tick(object sender, EventArgs e)

        {

            if (t > 0)

            {

                t--;

                int h = t / 3600;

                int m = (t / 60) % 60;

                int s = t % 60;

                string time = String.Format("{0:00}:{1:00}:{2:00}", h, m, s);

                this.Label1.Text = time;

            }

          

        }

    当前时间的显示

    对于这个就比较好实现了,我们可以借助.net提供的DateTime类型是一些方法来获得,DateTime里定义了很多关于时间的方法,很方便

    现在我们就来看看如何时间当前时间的显示:

      int s=DateTime.Now.Second;

      int h= DateTime.Now.Hour;

      int m= DateTime.Now.Minute;

    s++;

               string time = String.Format("{0:00}:{1:00}:{2:00}", h,m,s);

    这样就实现倒计时和当前时间不断走动的的效果!!!

              显示日期和时间的格式还有一种方式,静止不动的:

    DateTime.Now.Tostring(“yyyy-MM-dd hh:mm:ss”);

    2008-12-21 03:12:25

    多思考,多创新,才是正道!
  • 相关阅读:
    sharedWorker 实现多页面通信
    cookie跨域那些事儿
    event loop整理
    tsConfig/baseUrl -- 一键告别相对路径import
    vscode配置golang开发环境手把手描述篇
    Vue学习笔记二
    Vue学习笔记
    echarts迁移图动态加载
    病虫害可视化监测平台(一)
    昆虫识别开发进展APP(四)
  • 原文地址:https://www.cnblogs.com/shuang121/p/1969878.html
Copyright © 2011-2022 走看看