【发布时间】:2011-12-20 09:12:07
【问题描述】:
我用 C# 编写了一个小函数,这不是我的主要语言,所以对我来说有点陌生。
public bool CheckForKey(string key)
{
string strKeyTime = Decode(key);
//valid key will be current time +- 5 minutes
string strTheTime = DateTime.Now.ToString("HH:mm:ss tt");
if (strKeyTime == strTheTime)
{
return true;
}
else
{
return false;
}
}
我需要更改它以允许 5 分钟,所以 如果(strKeyTime == strTheTime) 需要是 if (strKeyTime == strTheTime + or - 5 分钟)
我的问题是匹配时间,因为它们是字符串,也许先将键(原始时间)转换回日期然后再做,但我对 c# 很陌生
【问题讨论】:
-
return strKeyTime == strTheTime;
标签: c# date time string-matching