当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET中TimeSpan的用法实例解析

ASP.NET中TimeSpan的用法实例解析

2017年12月12日  | 移动技术网IT编程  | 我要评论

心猿意马快播,大宁国际影城,马蒂斯简介

本文实例讲述了asp.net中timespan的用法,分享给大家供大家参考。具体如下:

asp.net 中,两个时间相减,得到一个 timespan 实例,timespan 有一些属性:days、totaldays、hours、totalhours、minutes、totalminutes、seconds、totalseconds、ticks,注意没有 totalticks。

一、timespan常量、字段

复制代码 代码如下:
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静态方法

复制代码 代码如下:
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();    //尝试从指定格式的字符串建立

三、timespan属性

复制代码 代码如下:
days;              //天部分 hours; //小时部分
milliseconds;      //毫秒部分
minutes;           //分部分
seconds;           //秒部分
ticks;             //tick 总数
totaldays;         //总天数
totalhours;        //总小时数
totalmilliseconds; //总毫秒数
totalminutes;      //总分钟数
totalseconds;      //总秒数

四、timespan方法

复制代码 代码如下:
add();       // + compareto(); //比对
duration();  //绝对值
equals();    //
negate();    //取反, + > -、- > +
subtract();  // -, add()的反操纵
tostring();  //格式化到字符串, .net 4.0 较之前版本有变动

五、timespan构建对象

复制代码 代码如下:
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
        );
}

六、timespan实例

时间 1 是 2010-1-2 8:43:35;
时间 2 是 2010-1-12 8:43:34。

用时间 2 减时间 1,得到一个 timespan 实例。

那么时间 2 比时间 1 多 9 天 23 小时 59 分 59 秒。

那么,days 就是 9,hours 就是 23,minutes 就是 59,seconds 就是 59。

再来看 ticks,tick 是一个计时周期,表示一百纳秒,即一千万分之一秒,那么 ticks 在这里表示总共相差多少个时间周期,即:9 * 24 * 3600 * 10000000 + 23 * 3600 * 10000000 + 59 * 60 * 10000000 + 59 * 10000000 = 8639990000000。3600 是一小时的秒数。

totaldays 就是把 ticks 换算成日数,即:8639990000000 / (10000000 * 24 * 3600) = 9.99998842592593。

totalhours 就是把 ticks 换算成小时数,即:8639990000000 / (10000000 * 3600) = 239.999722222222。

totalminutes 就是把 ticks 换算成分钟数,即:8639990000000 / (10000000 * 60) = 14399.9833333333。

totalseconds 就是把 ticks 换算成秒数,即:8639990000000 / (10000000) = 863999。

希望本文所述对大家的asp.net程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网