學用 ASP.Net 之 System.TimeSpan 結構

來源:互聯網
上載者:User
TimeSpan 表示一個時間間隔, 如:
protected void Button1_Click(object sender, EventArgs e){    DateTime dt1 = new DateTime(2010, 1, 2, 3, 4, 5);    DateTime dt2 = new DateTime(2011, 6, 6, 6, 6, 6);    TimeSpan ts = dt2 - dt1;    TextBox1.Text = ts.ToString(); //520.03:02:01 ( 這表示 520 天 3 小時 2 分 1 秒)}
其預設的字串格式:
protected void Button1_Click(object sender, EventArgs e){    TimeSpan ts = TimeSpan.MaxValue;    TextBox1.Text = ts.ToString(); //10675199.02:48:05.4775807 (天.時:分:秒.毫秒)}
成員:
/* 常量、欄位 */TimeSpan.MaxValue;            // 10675199.02:48:05.4775807TimeSpan.MinValue;            //-10675199.02:48:05.4775808TimeSpan.Zero;                //        0.00:00:00.0TimeSpan.TicksPerDay;         //一天的   Tick 數: 864000000000TimeSpan.TicksPerHour;        //一小時的 Tick 數: 36000000000TimeSpan.TicksPerMillisecond; //一毫秒的 Tick 數: 10000TimeSpan.TicksPerMinute;      //一分鐘的 Tick 數: 600000000TimeSpan.TicksPerSecond;      //一秒鐘的 Tick 數: 10000000/* 靜態方法 */TimeSpan.Compare();          //對比TimeSpan.Equals();           //=       TimeSpan.FromDays();         //從天數建立TimeSpan.FromHours();        //從小時數建立TimeSpan.FromMilliseconds(); //從毫秒數建立TimeSpan.FromMinutes();      //從分鐘數建立TimeSpan.FromSeconds();      //從秒數建立TimeSpan.FromTicks();        //從 Tick 數建立TimeSpan.Parse();            //從字串建立TimeSpan.ParseExact();       //從指定格式的字串建立TimeSpan.TryParse();         //嘗試從字串建立TimeSpan.TryParseExact();    //嘗試從指定格式的字串建立/* 屬性 */Days;              //天部分Hours;             //小時部分Milliseconds;      //毫秒部分Minutes;           //分部分Seconds;           //秒部分Ticks;             //Tick 總數TotalDays;         //總天數TotalHours;        //總小時數TotalMilliseconds; //總毫秒數TotalMinutes;      //總分鐘數TotalSeconds;      //總秒數/* 方法 */Add();       // +CompareTo(); //比對Duration();  //絕對值Equals();    //Negate();    //取反, + > -、- > +Subtract();  // -, Add()的反操縱ToString();  //格式化到字串, .Net 4.0 較之前版本有變動
構建對象:
protected void Button1_Click(object sender, EventArgs e){    TimeSpan t1 = new TimeSpan(864000000000);        //1.00:00:00    TimeSpan t2 = new TimeSpan(23, 59, 59);          //23:59:59    TimeSpan t3 = new TimeSpan(30, 23, 59, 59);      //30.23:59:59    TimeSpan t4 = new TimeSpan(30, 23, 59, 59, 999); //30.23:59:59.9990000    double f = 365.25;    TimeSpan t5 = TimeSpan.FromDays(f);                                         //365.06:00:00    TimeSpan t6 = TimeSpan.FromHours(f * 24);                                   //365.06:00:00    TimeSpan t7 = TimeSpan.FromMinutes(f * 24 * 60);                            //365.06:00:00    TimeSpan t8 = TimeSpan.FromSeconds(f * 24 * 60 * 60);                       //365.06:00:00    TimeSpan t9 = TimeSpan.FromMilliseconds(f * 24 * 60 * 60 * 1000);           //365.06:00:00    TimeSpan t0 = TimeSpan.FromTicks((long)(f * 24 * 60 * 60 * 1000 * 10000));  //365.06:00:00    TextBox1.Text = string.Format("{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n{6}\n{7}\n{8}\n{9}",            t1, t2, t3, t4, t5, t6, t7, t8, t9, t0        );}
Parse():
protected void Button1_Click(object sender, EventArgs e){    TimeSpan ts = TimeSpan.Parse("123.4:5:6.789");    TextBox1.Text = ts.ToString(); //123.04:05:06.7890000}
屬性測試:
protected void Button1_Click(object sender, EventArgs e){    TimeSpan ts = new TimeSpan(1, 2, 3, 4, 5);    int n1 = ts.Days;                 //1    int n2 = ts.Hours;                //2    int n3 = ts.Minutes;              //3    int n4 = ts.Seconds;              //4    int n5 = ts.Milliseconds;         //5    long tick = ts.Ticks;             //937840050000    double f1 = ts.TotalDays;         //1.08546302083333    double f2 = ts.TotalHours;        //26.0511125    double f3 = ts.TotalMinutes;      //1563.06675    double f4 = ts.TotalSeconds;      //93784.005    double f5 = ts.TotalMilliseconds; //93784005    TextBox1.Text = string.Format("{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n{6}\n{7}\n{8}\n{9}\n{10}",            n1, n2, n3, n4, n5, tick, f1, f2, f3, f4, f5        );}
Duration()、Negate():
protected void Button1_Click(object sender, EventArgs e){    DateTime dt1 = new DateTime(2010, 1, 2, 3, 4, 5);    DateTime dt2 = new DateTime(2011, 6, 6, 6, 6, 6);    TimeSpan ts1 = dt1 - dt2;      //-520.03:02:01    TimeSpan ts2 = ts1.Duration(); // 520.03:02:01    TimeSpan ts3 = ts2.Negate();   //-520.03:02:01    TimeSpan ts4 = -ts3;           // 520.03:02:01    TextBox1.Text = string.Concat(ts1, "\n", ts2, "\n", ts3, "\n", ts4);}
格式化輸出:
protected void Button1_Click(object sender, EventArgs e){    TimeSpan ts = new TimeSpan();    string s1 = ts.ToString();    //00:00:00    string s2 = ts.ToString("c"); //00:00:00    string s3 = ts.ToString("g"); //0:00:00    string s4 = ts.ToString("G"); //0:00:00:00.0000000    string s5 = string.Format("{0}天{1}時{2}分{3}秒{4}毫秒",             ts.TotalDays, ts.TotalHours, ts.TotalMinutes, ts.TotalSeconds, ts.TotalMilliseconds        );                        //0天0時0分0秒0毫秒    TextBox1.Text = string.Concat(s1, "\n", s2, "\n", s3, "\n", s4, "\n", s5);}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.