C# Timespan无法转换.ToInt16(elapsedSeconds))和.ToInt64(elapsedSeconds))错误#

C# Timespan无法转换.ToInt16(elapsedSeconds))和.ToInt64(elapsedSeconds))错误#,c#,unity3d,C#,Unity3d,尝试将变量elapseseconds转换为ToInt16和ToInt64时出错 TimeSpan cTime = new TimeSpan (0, 0, 0, Convert.ToInt16(elapsedSeconds)); 出现错误: OverflowException: Value is greater than Int16.MaxValue or less than Int16.MinValue System.Convert.ToInt16 (Int64 value) (at /Use

尝试将变量elapseseconds转换为ToInt16和ToInt64时出错

TimeSpan cTime = new TimeSpan (0, 0, 0, Convert.ToInt16(elapsedSeconds));
出现错误:

OverflowException: Value is greater than Int16.MaxValue or less than Int16.MinValue
System.Convert.ToInt16 (Int64 value) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Convert.cs:1093)
margaretSellTimer.OnResumeSession () (at Assets/script/margaretShop/margaretSellTimer.cs:148)
margaretSellTimer.Awake () (at Assets/script/margaretShop/margaretSellTimer.cs:28)
Assets/script/margaretShop/margaretSellTimer.cs(148,89): error CS1502: The best overloaded method match for `System.TimeSpan.TimeSpan(int, int, int, int)' has some invalid arguments

Assets/script/margaretShop/margaretSellTimer.cs(148,89): error CS1503: Argument `#4' cannot convert `long' expression to type `int'
我也试着把这句话改成这样:

TimeSpan cTime = new TimeSpan (0, 0, 0, Convert.ToInt64(elapsedSeconds));
这也得到了一个错误:

OverflowException: Value is greater than Int16.MaxValue or less than Int16.MinValue
System.Convert.ToInt16 (Int64 value) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Convert.cs:1093)
margaretSellTimer.OnResumeSession () (at Assets/script/margaretShop/margaretSellTimer.cs:148)
margaretSellTimer.Awake () (at Assets/script/margaretShop/margaretSellTimer.cs:28)
Assets/script/margaretShop/margaretSellTimer.cs(148,89): error CS1502: The best overloaded method match for `System.TimeSpan.TimeSpan(int, int, int, int)' has some invalid arguments

Assets/script/margaretShop/margaretSellTimer.cs(148,89): error CS1503: Argument `#4' cannot convert `long' expression to type `int'
我看到的是elapsedSeconds的值很大,所以我尝试使用ToInt64(长),但不能像那样转换

实际上,我想从cTime得到的是如下所示:

Seconds = cTime.Seconds;
            Minutes = cTime.Minutes;
            Hours = cTime.Hours;
            days = cTime.Days;
有什么想法吗


谢谢

如果您只需要从秒开始构建时间跨度,则可以使用此选项:

TimeSpan cTime = TimeSpan.FromSeconds(Convert.ToDouble(elapsedSeconds));

如果只需要从秒开始构建时间跨度,则可以使用此选项:

TimeSpan cTime = TimeSpan.FromSeconds(Convert.ToDouble(elapsedSeconds));

什么类型是
elapsedSeconds
以及哪个值保存它?它是长的elapsedSeconds=Convert.ToInt64(timestamp)-oldTimestamp;什么类型是
elapsedSeconds
以及哪个值保存它?它是长的elapsedSeconds=Convert.ToInt64(timestamp)-oldTimestamp;