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);}