C\#시간(몇 개의 상용 시간,프로그램 실행 시간,페이지 실행 시간)

5332 단어 C#
원본:http://www.cnblogs.com/tity/archive/2006/08/30/490605.html
 
1.DateTime
          DateTime now = System.DateTime.Now;

           now.ToString();                                                       //  : 2006/08/30 17:31:02

           now.ToString("yyyy-mm-dd hh:MM:ss");                //  : 2006-08-30 05:39:11

           now.ToString("yyyy-mm-dd HH:mm:ss");                //  : 2006-08-30 17:40:50

          System.DateTime.MaxValue.ToString();                   //  : 9999/12/31 23:59:59

          System.DateTime.MinValue.ToString();                   //  : 0001/01/01 0:00:00

         now.ToLongDateString();                                         //  : 2006 8 30 

         now.ToLongTimeString();                                         //  : 17:34:23

         now.ToShortTimeString();                                         //  : 17:34

         now.ToString() + " " + now.Millisecond.ToString();   //  : 2006/08/30 17:35:19 484


.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
           2.프로그램 실행 시간:(단위:  밀리 초
System.Diagnostics ;  //    

             int x = 0;

             int nu = 0;       

            Stopwatch sw = new Stopwatch();

            sw.Start();

            //    

             for (int i = 0; i < 1000000; i++)

            {

                x += i;

            }

            //    

            sw.Stop();

            this.label1.Text += ",sum=" + x.ToString();

            MessageBox.Show(sw.ElapsedMilliseconds.ToString());


.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
        3.페이지 실행 시간 계산 하기:        Global.aax.cs 파일 에 다음 코드 를 추가 합 니 다.       
 protected void Application_BeginRequest(Object sender, EventArgs e)

        {

            Application["StartTime"] = System.DateTime.Now;

        }

        protected void Application_EndRequest(Object sender, EventArgs e)

        {

            System.DateTime startTime = (System.DateTime)Application["StartTime"];

            System.DateTime endTime = System.DateTime.Now;

            System.TimeSpan ts = endTime - startTime;

            Response.Write("        :" + ts.Milliseconds + "   ");

        }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

좋은 웹페이지 즐겨찾기