一个TimeSpan对象都表示一个时间间隔 (持续时间的时间或时间),
单位为正数或负数的天数、 小时、 分钟、 秒和小数部分的第二个数字。
TimeSpan结构还可以用于表示一天时间,但仅,如果与某一特定日期无关的时间。
否则为DateTime或DateTimeOffset应改为使用结构。

TimeSpan构造函数
1.TimeSpan(Int64): 将TimeSpan结构的新实例初始化为指定的刻度数。
2.TimeSpan(Int32, Int32, Int32): 将TimeSpan结构的新实例初始化为指定的小时数、分钟数和秒数。
3.TimeSpan(Int32, Int32, Int32, Int32): 将TimeSpan结构的新实例初始化为指定的天数、小时数、分钟数和秒数。
4.TimeSpan(Int32, Int32, Int32, Int32, Int32): 将TimeSpan结构的新实例初始化为指定的天数、小时数、分钟数、秒数和毫秒数。

1.TimeSpan(Int64),public TimeSpan (long ticks);
将TimeSpan 结构的新实例初始化为指定的刻度数
ticks 100 毫微秒为单位表示的时间段。

TimeSpan inverval2 = new TimeSpan();
Console.WriteLine(inverval2.ToString());//00:00:00.0000020

2.TimeSpan(Int32, Int32, Int32),public TimeSpan (int hours, int minutes, int seconds);
将TimeSpan 结构的新实例初始化为指定的小时数、分钟数和秒数。

TimeSpan inverval3 = new TimeSpan(, , );
Console.WriteLine(inverval3.ToString());//15:45:30

3.TimeSpan(Int32, Int32, Int32, Int32),public TimeSpan(int days, int hours, int minutes, int seconds);

将TimeSpan 结构的新实例初始化为指定的天数、小时数、分钟数和秒数。

TimeSpan inverval4_1 = new TimeSpan(, , , );
TimeSpan inverval4_2 = new TimeSpan(-, , , );
TimeSpan inverval4_3 = new TimeSpan(-, -, -, -);
TimeSpan inverval4_4 = new TimeSpan(, , , );
Console.WriteLine("{0,-35}{1,20}", "new TimeSpan(10, 20, 30, 40)", inverval4_1.ToString());
Console.WriteLine("{0,-35}{1,20}", "new TimeSpan(-10, 20, 30, 40)", inverval4_2.ToString());
Console.WriteLine("{0,-35}{1,20}", "new TimeSpan(-10, -20, -30, -40)", inverval4_3.ToString());
Console.WriteLine("{0,-35}{1,20}", "new TimeSpan(0, 49, 30, 40)", inverval4_4.ToString()); new TimeSpan(, , , ) //10.20:30:40
new TimeSpan(-, , , ) //-9.03:29:20
new TimeSpan(-, -, -, -) //-10.20:30:40
new TimeSpan(, , , ) //2.01:30:40

4.TimeSpan(Int32, Int32, Int32, Int32, Int32),public TimeSpan(int days, int hours, int minutes, int seconds, int milliseconds);

将TimeSpan 结构的新实例初始化为指定的天数、小时数、分钟数和秒数。

TimeSpan inverval5 = new TimeSpan(, , , ,);
Console.WriteLine("{0,-35}{1,20}", "new TimeSpan(10, 20, 30, 40,20)", inverval5.ToString());
new TimeSpan(, , , ,) //10.20:30:40.0200000

TimeSpan 字段
 Maximum:表示最大的TimeSpan值,此字段为只读,此字段的值等效于Int64.MaxValue计时周期数。
                   此值的字符串表示形式是正 10675199.02:48:05.4775807 或略微超过 10675199 天。
 Maximum:表示最小的TimeSpan值,此字段为只读,此字段的值等效于Int64.MinValue计时周期数。
      此值的字符串表示形式是负 10675199.02:48:05.4775808 或负略微超过 10675199 天。
 Zero:表示零TimeSpan值,此字段为只读
    因为的值Zero字段是TimeSpan对象,表示零个时间值,将其与其他进行比较TimeSpan对象以确定
      是否后者表示正的非零或负时间间隔。 此外可以使用此字段来初始化TimeSpan为零个时间值的对象。
 TicksPerDay:表示一天中的刻度数。 此字段为常数。此常量的值是 864 亿个;也就是说,864,000,000,000。
 TicksPerHour:表示 1 小时的刻度数。 此字段为常数。此常量的值是 36 亿个;也就是说,36,000,000,000。
 icksPerMinute:表示 1 分钟的刻度数。 此字段为常数。此常量的值为 600 万;也就是说,600000000。
 TicksPerSecond:表示 1 秒的刻度数。此常量的值为 1000 万;也就是说,10,000,000。
 TicksPerMinute:表示 1 分钟的刻度数。 此字段为常数。此常量的值为 600 万;也就是说,600000000。
 TicksPerMillisecond:表示 1 毫秒的刻度数。 此字段为常数。此常量的值是 1 万。它是 10,000。

            const string numberFmt = "{0,-22}{1,18:N0}";
const string timeFmt = "{0,-22}{1,26}"; Console.WriteLine("This example of the fields of the TimeSpan class" + "\ngenerates the following output.\n");
Console.WriteLine(numberFmt, "Field", "Value");
Console.WriteLine(numberFmt, "-----", "-----"); // Display the maximum, minimum, and zero TimeSpan values.
Console.WriteLine(timeFmt, "Maximum TimeSpan", Align(TimeSpan.MaxValue));
Console.WriteLine(timeFmt, "Minimum TimeSpan", Align(TimeSpan.MinValue));
Console.WriteLine(timeFmt, "Zero TimeSpan", Align(TimeSpan.Zero));
Console.WriteLine(); // Display the ticks-per-time-unit fields.
Console.WriteLine(numberFmt, "Ticks per day", TimeSpan.TicksPerDay);
Console.WriteLine(numberFmt, "Ticks per hour", TimeSpan.TicksPerHour);
Console.WriteLine(numberFmt, "Ticks per minute", TimeSpan.TicksPerMinute);
Console.WriteLine(numberFmt, "Ticks per second", TimeSpan.TicksPerSecond);
Console.WriteLine(numberFmt, "Ticks per millisecond", TimeSpan.TicksPerMillisecond); /// <summary>
///对齐方法
/// </summary>
/// <param name="interval"></param>
/// <returns></returns>
static string Align(TimeSpan interval)
{
string intervalStr = interval.ToString();
int pointIndex = intervalStr.IndexOf(':'); pointIndex = intervalStr.IndexOf('.', pointIndex);
if (pointIndex < ) intervalStr += " ";
return intervalStr;
}

未完待续。。。。

TimeSpan时间间隔的更多相关文章

  1. TimeSpan时间间隔详解

    1 public string GetShiXian(string fadanshijian) 2 { 3 string result,chulishijian; 4 5 DateTime fdTim ...

  2. C#基础:命令解析

    1.普通格式命令的解析 例如: RENA<SP>E:\\A.txt<SP>C:\\B.txt<CRLF> (SP -> 空格,CRLF -> 回车加换行 ...

  3. JAVA 文本 TXT 操作工具类

    import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; imp ...

  4. JAVA Date超强工具类,可直接取代util.Date使用

    package net.maxt.util; import java.text.DateFormat; import java.text.ParseException; import java.tex ...

  5. Unity3D实现DoubleClick的一种方法

    代码简单粗暴如下: void OnMouseDown() { ) { t2 = DateTime.Now; , , , , )) //时间间隔小于500ms,认为是双击 { // 双击后的操作 } t ...

  6. Wpf自动滚动效果

    一.思路 1.使用ScrollView的Scroll.ScrollToVerticalOffset(offset)方法进行滚动 2.ScrollView中放置2个ListView,第一个滚动出边界后, ...

  7. 使用DateAdd方法向指定日期添加一段时间间隔,使用TimeSpan对象获取时间间隔

    一:使用DateAdd方法向指定日期添加一段时间间隔,截图 二:代码 using System; using System.Collections.Generic; using System.Comp ...

  8. [转载]C# TimeSpan 计算时间差(时间间隔)

    TimeSpan 结构  表示一个时间间隔. 命名空间:System 程序集:mscorlib(在 mscorlib.dll 中) 说明: 1.DateTime值类型代表了一个从公元0001年1月1日 ...

  9. 使用TimeSpan对象获取时间间隔

    实现效果: 关键知识: TimeSpan对象表是时间间隔或持续时间,两个DateTime对象相减,则会得到一个TimeSpan对象 使用其days ,hours,minutes等属性 实现代码: pr ...

随机推荐

  1. input[type="radio"]自定义样式

    input为radio时,虽然会有默认选中的样式,但是并不符合大多数项目的需求,我们的目标是可以随心所欲自定义它的样式.怎么做呢?其实很简单,只要抓住3点.分别是1.label 2.隐藏自带样式 3. ...

  2. C++ qsort() 函数调用时实参与形参不兼容的问题解决

    <剑指OFFER>刷题笔记 —— 扑克牌顺子 LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自 ...

  3. MVC中的过滤器/拦截器怎么写

    创建一个AuthenticateFilterAttribute(即过滤器/拦截器) 引用System.Web.Mvc; public class AuthenticateFilterAttribute ...

  4. 5种你未必知道的JS和CSS交互的方法

    随着浏览器不断的升级改进,CSS和JavaScript之间的界限越来越模糊.本来它们是负责着完全不同的功能,但最终,它们都属于网页前端技术,它们需要相互密切的合作.我们的网页中都有.js文件和.css ...

  5. 刷leetcode是什么样的体验?【转】

    转自:https://www.zhihu.com/question/32322023 刷leetcode是什么样的体验? https://leetcode.com/ 1 条评论   默认排序 按时间排 ...

  6. CocoaPods Setting up CocoaPods master repo无反应时的处理

    Setting up CocoaPods master repo,半天没有任何反应.原因无他,因为那堵墙阻挡了cocoapods.org...gitcafe和oschina都是国内的服务器,可以用它们 ...

  7. 一、Ubuntu 简介

    Ubuntu 是一个Linux 系统 Apt-Get apt-get 命令是一个强大的命令行工具,用于同 Ubuntu 的 Advanced Packaging Tool (APT) 一起执行诸如安装 ...

  8. 洛谷 P1478 陶陶摘苹果(升级版)【贪心/结构体排序/可用01背包待补】

    [链接]:https://www.luogu.org/problemnew/show/P1478 题目描述 又是一年秋季时,陶陶家的苹果树结了n个果子.陶陶又跑去摘苹果,这次她有一个a公分的椅子.当他 ...

  9. 几个相似的DP题

    HDU1398 题意:把一个整数分拆成1.4.9.16.…….256.289(注意:只到289)这17个完全平方数的和,有几种方法. 解法不用说自然是DP,因为搜索显然超时. (这样的题我一般不敢开i ...

  10. Codeforces 777D Cloud of Hashtags(贪心)

    题目链接 Cloud of Hashtags 题目还是比较简单的,直接贪心,但是因为我有两个细节没注意,所以FST了: 1.用了cin读入,但是没有加 std::ios::sync_with_stdi ...