怎样将TimeSpan转换成Int类型,非常感谢!

wuhz520 2009-03-23 10:54:21
怎样将TimeSpan转换成Int类型,非常感谢!
...全文
2677 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
gushangmoqi 2011-03-08
  • 打赏
  • 举报
回复
很简单,
TimeSpan a;
double b= a.TotalMinutes / Convert.ToDouble(1);
int i=Convert.ToInt32(b);
crystalsky21504119 2009-03-24
  • 打赏
  • 举报
回复
接分
ncowboy 2009-03-24
  • 打赏
  • 举报
回复
看看MSDN吧。
http://msdn.microsoft.com/zh-cn/library/system.timespan.days.aspx
zzxap 2009-03-24
  • 打赏
  • 举报
回复
TimeSpan是16进制的
zzxap 2009-03-24
  • 打赏
  • 举报
回复
可以直接告诉你:不能。
he_hawk 2009-03-24
  • 打赏
  • 举报
回复
TimeSpan 实例的值表示一个时间段。该值是实例中包含的刻度数,其范围可以在 Int64.MinValue 到 Int64.MaxValue 之间。一个刻度是可以指定的最小时间单位,等于 100 毫微秒。刻度数和 TimeSpan 的值都可以指定为正值或负值。

例如,初始化为 1.0e+13 刻度的 TimeSpan 表示“11.13:46:40”,即 11 天,13 小时,46 分钟和 40 秒。


MSDN上的说明。
he_hawk 2009-03-24
  • 打赏
  • 举报
回复
up
hanyu0528 2009-03-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 OKILOVE 的回复:]
TimeSpan是一个包含天数、小时、分、秒和毫秒的结构,是不能直接转换为int类型的。
TimeSpan.Days返回int类型的天数,TimeSpan.Hours返回int类型的小时数,一次可以得到int类型分钟、秒和毫秒。
[/Quote]
up
birdlonger 2009-03-24
  • 打赏
  • 举报
回复
mark
chen2319 2009-03-24
  • 打赏
  • 举报
回复
先用TimeSpan.ToString 方法
// Example of the TimeSpan.Parse( string ) and TimeSpan.ToString( ) 
// methods.
using System;

class TSParseToStringDemo
{
static void ParseNDisplayTimeSpan( string intervalStr )
{
// Write the first part of the output line.
Console.Write( "{0,20} ", intervalStr );

// Parse the parameter, and then convert it back to a string.
try
{
TimeSpan intervalVal = TimeSpan.Parse( intervalStr );
string intervalToStr = intervalVal.ToString( );

// Pad the end of the TimeSpan string with spaces if it
// does not contain milliseconds.
int pIndex = intervalToStr.IndexOf( ':' );
pIndex = intervalToStr.IndexOf( '.', pIndex );
if( pIndex < 0 ) intervalToStr += " ";

Console.WriteLine( "{0,21}", intervalToStr );
}
catch( Exception ex )
{
// If Parse throws an exception, write the message.
Console.WriteLine( ex.Message );
}
}

static void Main( )
{
Console.WriteLine(
"This example of TimeSpan.Parse( string ) and \n" +
"TimeSpan.ToString( ) " +
"generates the following output.\n" );
Console.WriteLine( "{0,20} {1,21}",
"String to Parse", "TimeSpan or Exception" );
Console.WriteLine( "{0,20} {1,21}",
"---------------", "---------------------" );

ParseNDisplayTimeSpan( "0" );
ParseNDisplayTimeSpan( "14" );
ParseNDisplayTimeSpan( "1:2:3" );
ParseNDisplayTimeSpan( "0:0:0.250" );
ParseNDisplayTimeSpan( "10.20:30:40.50" );
ParseNDisplayTimeSpan( "99.23:59:59.9999999" );
ParseNDisplayTimeSpan( "0023:0059:0059.0099" );
ParseNDisplayTimeSpan( "24:0:0" );
ParseNDisplayTimeSpan( "0:60:0" );
ParseNDisplayTimeSpan( "0:0:60" );
ParseNDisplayTimeSpan( "10:" );
ParseNDisplayTimeSpan( ":10" );
ParseNDisplayTimeSpan( "10:20:" );
ParseNDisplayTimeSpan( ".123" );
ParseNDisplayTimeSpan( "10." );
ParseNDisplayTimeSpan( "10.12" );
}
}

/*
This example of TimeSpan.Parse( string ) and
TimeSpan.ToString( ) generates the following output.

String to Parse TimeSpan or Exception
--------------- ---------------------
0 00:00:00
14 14.00:00:00
1:2:3 01:02:03
0:0:0.250 00:00:00.2500000
10.20:30:40.50 10.20:30:40.5000000
99.23:59:59.9999999 99.23:59:59.9999999
0023:0059:0059.0099 23:59:59.0099000
24:0:0 TimeSpan overflowed because the duration is too long.
0:60:0 TimeSpan overflowed because the duration is too long.
0:0:60 TimeSpan overflowed because the duration is too long.
10: Input string was not in a correct format.
:10 Input string was not in a correct format.
10:20: Input string was not in a correct format.
.123 Input string was not in a correct format.
10. Input string was not in a correct format.
10.12 Input string was not in a correct format.
*/


然后你想怎么弄就怎么弄。
terry 2009-03-24
  • 打赏
  • 举报
回复
有2楼,我jf。
stubbornleaf 2009-03-24
  • 打赏
  • 举报
回复
你的意思可能是想用int在两个TimeSpan之间转换吧?
可以用Ticks属性,不过是个long的,你可以用它来创建新的TimeSpan实例。
lg3605119 2009-03-24
  • 打赏
  • 举报
回复
二楼没错
OKILOVE 2009-03-24
  • 打赏
  • 举报
回复
TimeSpan是一个包含天数、小时、分、秒和毫秒的结构,是不能直接转换为int类型的。
TimeSpan.Days返回int类型的天数,TimeSpan.Hours返回int类型的小时数,一次可以得到int类型分钟、秒和毫秒。
EveryCase 2009-03-24
  • 打赏
  • 举报
回复
顶~~~~~~~~~~~~

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧