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.4775807
TimeSpan.MinValue; //-10675199.02:48:05.4775808
TimeSpan.Zero; // 0.00:00:00.0
TimeSpan.TicksPerDay; //一天的 Tick 数: 864000000000
TimeSpan.TicksPerHour; //一小时的 Tick 数: 36000000000
TimeSpan.TicksPerMillisecond; //一毫秒的 Tick 数: 10000
TimeSpan.TicksPerMinute; //一分钟的 Tick 数: 600000000
TimeSpan.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);
}