asp.net中timespan的用法
來源:易賢網(wǎng) 閱讀:792 次 日期:2014-10-21 09:52:37
溫馨提示:易賢網(wǎng)小編為您整理了“asp.net中timespan的用法”,方便廣大網(wǎng)友查閱!

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 數(shù): 864000000000

timespan.ticksperhour; //一小時的 tick 數(shù): 36000000000

timespan.tickspermillisecond; //一毫秒的 tick 數(shù): 10000

timespan.ticksperminute; //一分鐘的 tick 數(shù): 600000000

timespan.tickspersecond; //一秒鐘的 tick 數(shù): 10000000

二、timespan靜態(tài)方法

timespan.compare(); //對比

timespan.equals(); //=

timespan.fromdays(); //從天數(shù)建立

timespan.fromhours(); //從小時數(shù)建立

timespan.frommilliseconds(); //從毫秒數(shù)建立

timespan.fromminutes(); //從分鐘數(shù)建立

timespan.fromseconds(); //從秒數(shù)建立

timespan.fromticks(); //從 tick 數(shù)建立

timespan.parse(); //從字符串建立

timespan.parseexact(); //從指定格式的字符串建立

timespan.tryparse(); //嘗試從字符串建立

timespan.tryparseexact(); //嘗試從指定格式的字符串建立

三、timespan屬性

days; //天部分 hours; //小時部分

milliseconds; //毫秒部分

minutes; //分部分

seconds; //秒部分

ticks; //tick 總數(shù)

totaldays; //總天數(shù)

totalhours; //總小時數(shù)

totalmilliseconds; //總毫秒數(shù)

totalminutes; //總分鐘數(shù)

totalseconds; //總秒數(shù)

四、timespan方法

add(); // + compareto(); //比對

duration(); //絕對值

equals(); //

negate(); //取反, + > -、- > +

subtract(); // -, add()的反操縱

tostring(); //格式化到字符串, .net 4.0 較之前版本有變動

五、timespan構(gòu)建對象

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

);

}

求離最近發(fā)表時間的函數(shù)

public string datestringfromnow(datetime dt)

{

timespan span = datetime.now - dt;

if (span.totaldays > 60)

{

return dt.toshortdatestring();

}

else if ( span.totaldays > 30 )

{

return 1個月前;

}

else if (span.totaldays > 14)

{

return 2周前;

}

else if (span.totaldays > 7)

{

return 1周前;

}

else if (span.totaldays > 1)

{

return string.format({0}天前, (int)math.floor(span.totaldays));

}

else if (span.totalhours > 1)

{

return string.format({0}小時前, (int)math.floor(span.totalhours));

}

else if (span.totalminutes > 1)

{

return string.format({0}分鐘前, (int)math.floor(span.totalminutes));

}

else if (span.totalseconds >= 1)

{

return string.format({0}秒前, (int)math.floor(span.totalseconds));

}

else

{

return 1秒前;

}

}

datetime和timespan中的常用成員及其說明

屬性說明

date獲取此實例的日期部分。

day獲取此實例所表示的日期為該月中的第幾天。

dayofweek獲取此實例所表示的日期是星期幾。

dayofyear獲取此實例所表示的日期是該年中的第幾天。

hour獲取此實例所表示日期的小時部分。

millisecond獲取此實例所表示日期的毫秒部分。

minute獲取此實例所表示日期的分鐘部分。

month獲取此實例所表示日期的月份部分。

now創(chuàng)建一個datetime實例,它是此計算機上的當(dāng)前本地日期和時間。

second獲取此實例所表示日期的秒部分。

timeofday獲取此實例的當(dāng)天的時間。

today獲取當(dāng)前日期。

year獲取此實例所表示日期的年份部分。

表01:datetime類的常用屬性及其說明

方法說明

add將指定的timespan的值加到此實例的值上。

adddays將指定的天數(shù)加到此實例的值上。

addhours將指定的小時數(shù)加到此實例的值上。

addmilliseconds將指定的毫秒數(shù)加到此實例的值上。

addminutes將指定的分鐘數(shù)加到此實例的值上。

addmonths將指定的月份數(shù)加到此實例的值上。

addseconds將指定的秒數(shù)加到此實例的值上。

addyears將指定的年份數(shù)加到此實例的值上。

daysinmonth返回指定年份中指定月份的天數(shù)。

isleapyear返回指定的年份是否為閏年的指示。

parse將日期和時間的指定字符串表示轉(zhuǎn)換成其等效的datetime實例。

subtract從此實例中減去指定的時間或持續(xù)時間。

tolongdatestring將此實例的值轉(zhuǎn)換為其等效的長日期字符串表示形式。

tolongtimestring將此實例的值轉(zhuǎn)換為其等效的長時間字符串表示形式。

toshorttimestring將此實例的值轉(zhuǎn)換為其等效的短時間字符串表示形式。

toshortdatestring將此實例的值轉(zhuǎn)換為其等效的短日期字符串表示形式。

表02:datetime結(jié)構(gòu)的常用方法及其說明

表03和表04分別是timespan結(jié)構(gòu)的常用屬性和常用方法及其說明:

屬性說明

days獲取由此實例表示的整天數(shù)。

hours獲取由此實例表示的整小時數(shù)。

milliseconds獲取由此實例表示的整毫秒數(shù)。

minutes獲取由此實例表示的整分鐘數(shù)。

seconds獲取由此實例表示的整秒數(shù)。

ticks獲取用刻度表示的此實例的值。

totaldays獲取以整天數(shù)和天的小數(shù)部分表示的此實例的值。

totalhours獲取以整小時數(shù)和小時的小數(shù)部分表示的此實例的值。

totalmilliseconds獲取以整毫秒數(shù)和毫秒的小數(shù)部分表示的此實例的值。

totalminutes獲取以整分鐘數(shù)和分鐘的小數(shù)部分表示的此實例的值。

totalseconds獲取以整秒數(shù)和秒的小數(shù)部分表示的此實例的值。

表03:timespan結(jié)構(gòu)的常用屬性及其說明

方法說明

add將指定的timespan添加到此實例中。

duration返回其值為此實例的絕對值的timespan。

fromdays返回表示指定天數(shù)的timespan,其中對天數(shù)的指定精確到最接近的毫秒。

fromhours返回表示指定小時數(shù)的timespan,其中對小時數(shù)的指定精確到最接近的毫秒。

frommilliseconds返回表示指定毫秒數(shù)的timespan。

fromminutes返回表示指定分鐘數(shù)的timespan,其中對分鐘數(shù)的指定精確到最接近的毫秒。

fromseconds返回表示指定秒數(shù)的timespan,其中對秒數(shù)的指定精確到最接近的毫秒。

subtract從此實例中減去指定的timespan。

表04:timespan結(jié)構(gòu)的常用方法及其說明

更多信息請查看IT技術(shù)專欄

更多信息請查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機網(wǎng)站地址:asp.net中timespan的用法

2025國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)