如何计算 2 个系统时间与一个来自字符串的系统时间之间的差异
how to Calculate the differents between 2 system times with one from string
我正在尝试计算两次之间的秒数。
有 1 个时间值来自我的数据库中的字符串,我尝试在 Unity 中将其转换为 System.DateTime
但没有成功。
这是来自数据库的字符串:3-5-2022 10:20:45
public string starttime = "3-5-2022 10:20:45";
public void StakingProfitCheck()
{
StartCoroutine(StakingCheck());
}
IEnumerator StakingCheck()
{
WWW pwww = new WWW("DatabasePage");
yield return new WaitForSeconds(0.2f);
yield return pwww;
starttime = pwww.text;
Timecalculator();
}
void Timecalculator()
{
System.TimeSpan ts = System.DateTime.UtcNow - Convert.ToDateTime(starttime);
Debug.Log (ts.Seconds.ToString());
}
出现此错误:
FormatException: String was not recognized as a valid DateTime.
System.DateTimeParse.Parse (System.ReadOnlySpan`1[T] s,
System.Globalization.DateTimeFormatInfo dtfi,
System.Globalization.DateTimeStyles styles) (at
:0) System.DateTime.Parse
(System.String s, System.IFormatProvider provider) (at
:0) System.Convert.ToDateTime
(System.String value) (at :0)
StakingMenuScript.Timecalculator (System.String StartTime) (at
Assets/Scripts/StakingMenuScript.cs:54)
StakingMenuScript+d__5.MoveNext () (at
Assets/Scripts/StakingMenuScript.cs:50)
UnityEngine.SetupCoroutine.InvokeMoveNext
(System.Collections.IEnumerator enumerator, System.IntPtr
returnValueAddress) (at <13e6546058e340ada820e34dce3b245e>:0)
如何计算以秒为单位的时差???
您的问题是解析日期不正确。
您可以使用 DateTime.Parse
。假设您的格式是德语,您的代码应该如下所示。
private void Timecalculator(string startTime)
{
var start = DateTime.Parse(startTime, new CultureInfo("de-DE"), DateTimeStyles.None);
var difference = DateTime.UtcNow.Subtract(start);
}
您以不恰当的方式解析日期。您可以尝试使用 ParseExact
和您期望的 string
日期格式。
var dateString = "3-5-2022 10:20:45";
var format = "d-M-yyyy HH:mm:ss";
var result = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture);
Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
https://docs.microsoft.com/pl-pl/dotnet/api/system.datetime.parseexact?view=net-6.0
要获取时差,您可以使用以下语法:
System.TimeSpan diff = secondDate.Subtract(firstDate);
我正在尝试计算两次之间的秒数。
有 1 个时间值来自我的数据库中的字符串,我尝试在 Unity 中将其转换为 System.DateTime
但没有成功。
这是来自数据库的字符串:3-5-2022 10:20:45
public string starttime = "3-5-2022 10:20:45";
public void StakingProfitCheck()
{
StartCoroutine(StakingCheck());
}
IEnumerator StakingCheck()
{
WWW pwww = new WWW("DatabasePage");
yield return new WaitForSeconds(0.2f);
yield return pwww;
starttime = pwww.text;
Timecalculator();
}
void Timecalculator()
{
System.TimeSpan ts = System.DateTime.UtcNow - Convert.ToDateTime(starttime);
Debug.Log (ts.Seconds.ToString());
}
出现此错误:
FormatException: String was not recognized as a valid DateTime. System.DateTimeParse.Parse (System.ReadOnlySpan`1[T] s, System.Globalization.DateTimeFormatInfo dtfi, System.Globalization.DateTimeStyles styles) (at :0) System.DateTime.Parse (System.String s, System.IFormatProvider provider) (at :0) System.Convert.ToDateTime (System.String value) (at :0) StakingMenuScript.Timecalculator (System.String StartTime) (at Assets/Scripts/StakingMenuScript.cs:54) StakingMenuScript+d__5.MoveNext () (at Assets/Scripts/StakingMenuScript.cs:50) UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <13e6546058e340ada820e34dce3b245e>:0)
如何计算以秒为单位的时差???
您的问题是解析日期不正确。
您可以使用 DateTime.Parse
。假设您的格式是德语,您的代码应该如下所示。
private void Timecalculator(string startTime)
{
var start = DateTime.Parse(startTime, new CultureInfo("de-DE"), DateTimeStyles.None);
var difference = DateTime.UtcNow.Subtract(start);
}
您以不恰当的方式解析日期。您可以尝试使用 ParseExact
和您期望的 string
日期格式。
var dateString = "3-5-2022 10:20:45";
var format = "d-M-yyyy HH:mm:ss";
var result = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture);
Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
https://docs.microsoft.com/pl-pl/dotnet/api/system.datetime.parseexact?view=net-6.0
要获取时差,您可以使用以下语法:
System.TimeSpan diff = secondDate.Subtract(firstDate);