为什么数据类型转换<;long-oldTimestamp=Convert.ToInt64(值)>;返回错误?统一c#

为什么数据类型转换<;long-oldTimestamp=Convert.ToInt64(值)>;返回错误?统一c#,c#,unity3d,C#,Unity3d,运行此脚本时出错: long timestamp = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds; string values = PlayerPrefs.GetString("margaretSellTimer1"); long oldTimestamp = Convert.ToInt64(values); long elapsedSeconds = Convert.ToInt64(timestam

运行此脚本时出错:

long timestamp = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds;
string values = PlayerPrefs.GetString("margaretSellTimer1");
long oldTimestamp = Convert.ToInt64(values);
long elapsedSeconds = Convert.ToInt64(timestamp) - oldTimestamp;

string value = PlayerPrefs.GetString("margaretSellTimer");
long oldTickTime = Convert.ToInt64(value);
oldTickTime = oldTickTime + elapsedSeconds;
该行:

long timestamp = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds;
long oldTimestamp = Convert.ToInt64(values);
返回错误:

Assets/script/margaretSellTimer.cs(138,22): error CS0266: Cannot implicitly convert type double to long. An explicit conversion exists (are you missing a cast?)
Input string was not in the correct format
该行:

long timestamp = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds;
long oldTimestamp = Convert.ToInt64(values);
返回错误:

Assets/script/margaretSellTimer.cs(138,22): error CS0266: Cannot implicitly convert type double to long. An explicit conversion exists (are you missing a cast?)
Input string was not in the correct format
为什么这是一个错误?我做了一个转换器


谢谢

错误告诉您,
TotalSeconds
double
而不是
long
。只需将
长时间戳
替换为
双时间戳

double timestamp = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds;
或者使用(long)将
double
转换为
long

您还可以按照注释中提到的Jeppe的方式进行,这样可以防止不必要的数据类型转换

long timestamp = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).Ticks / TimeSpan.TicksPerSecond;

错误是告诉您,
TotalSeconds
double
而不是
long
。只需将
长时间戳
替换为
双时间戳

double timestamp = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds;
或者使用(long)将
double
转换为
long

您还可以按照注释中提到的Jeppe的方式进行,这样可以防止不必要的数据类型转换

long timestamp = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).Ticks / TimeSpan.TicksPerSecond;

对于您的铸造错误:

如果您查看的文档,您将看到
TimeSpan.TotalSeconds
是一个
double
。c#不允许隐式转换为
long
。因此,您可以将时间戳的数据类型更改为
double timestamp
,或者显式强制转换

long timestamp = (long) (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds;
对于您的转换错误:


value=PlayerPrefs.GetString(“margaretSellTimer”)
的结果无法解析为
long
,因为它包含非法字符或例如可能是双精度字符。在转换行中设置断点,并检查
值的内容

对于您的铸造错误:

如果您查看的文档,您将看到
TimeSpan.TotalSeconds
是一个
double
。c#不允许隐式转换为
long
。因此,您可以将时间戳的数据类型更改为
double timestamp
,或者显式强制转换

long timestamp = (long) (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds;
对于您的转换错误:


value=PlayerPrefs.GetString(“margaretSellTimer”)
的结果无法解析为
long
,因为它包含非法字符或例如可能是双精度字符。在转换行中设置断点,并检查
值的内容

是的,那是工人。。谢谢,我只是不知道问题出在哪里。可以使用:
(DateTime.UtcNow-newdatetime(1970,1,1,0,0,0,0)).Ticks/TimeSpan.TicksPerSecond
。这避免了通过浮点运算。“干净多了!”杰佩斯蒂格尼尔森说,这是另一个好办法。将其添加到解决方案中。@DennisLiu现在我开始考虑它,BCL现在已经构建了对此的支持(自.NET 4.6,Visual Studio 2015)。只需说
long timestamp=DateTimeOffset.UtcNow.ToUnixTimeSeconds()。那就不需要你自己的“公式”了@JeppeStigNielsen他还不能使用C#NET 4.6中的任何东西。Unity仍在使用C#3.5版本。是的,那是工人。。谢谢,我只是不知道问题出在哪里。可以使用:
(DateTime.UtcNow-newdatetime(1970,1,1,0,0,0,0)).Ticks/TimeSpan.TicksPerSecond
。这避免了通过浮点运算。“干净多了!”杰佩斯蒂格尼尔森说,这是另一个好办法。将其添加到解决方案中。@DennisLiu现在我开始考虑它,BCL现在已经构建了对此的支持(自.NET 4.6,Visual Studio 2015)。只需说
long timestamp=DateTimeOffset.UtcNow.ToUnixTimeSeconds()。那就不需要你自己的“公式”了@JeppeStigNielsen他还不能使用C#NET 4.6中的任何东西。Unity仍在使用C#3.5版本。