C# 时间匹配

发布于 2024-12-13 02:26:51 字数 545 浏览 2 评论 0原文

我用 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 分钟时间,所以 if (strKeyTime == strTheTime) 需要是 if (strKeyTime == strTheTime + or - 5 分钟)

我的问题是匹配时间,因为它们是字符串,也许先将键(原始时间)转换回日期,然后再执行,但我对 c# 很陌生

I have written a small function in C# which isn't my main launguage so is coming across a little foreign to me.

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;
    }
}

I need to alter this to allow for 5 minutes, so
if (strKeyTime == strTheTime)
needs to be
if (strKeyTime == strTheTime + or - 5 minutes)

my problem is matching the times as they are strings, perhaps convert key(original time) back to a date first and then do it, but I am pretty new to c#

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爺獨霸怡葒院 2024-12-20 02:26:51

如果您将它们都转换(或保留)为 DateTimes,您可以使用 TimeSpan

TimeSpan delta = dateTime1 - dateTime2;
if (Math.Abs(delta.TotalMinutes) <= 5) { ... }

研究使用 DateTime.ParseExact (或任何 Parse... 方法)来解析你的 strKeyTime,然后执行与上面类似的操作。

If you convert (or keep) them both to DateTimes you can use TimeSpan:

TimeSpan delta = dateTime1 - dateTime2;
if (Math.Abs(delta.TotalMinutes) <= 5) { ... }

Look into using the DateTime.ParseExact (or any of the Parse... methods) to parse your strKeyTime, and then do something similar to the above.

哭泣的笑容 2024-12-20 02:26:51

要将发送的字符串转换为等效的 DateTime 值,请使用以下代码:

var keyDateTime = Convert.ToDateTime(strKeyTime);
var strTheTime = DateTime.Now

从这里,您可以使用该值与原始时间值进行比较,如下所示:

if (keyDateTime == strTheTime || (keyDateTime > strTheTime && keyDateTime < strTheTime.AddMinutes(5))
{
    return true;
}

上一个代码块将首先检查我们是否获得完全匹配,或者发送到该方法的时间介于原始时间和额外 5 分钟的时间偏移之间。

就是这样,如果这不是您需要的,请告诉我,以便我可以为您更新我的答案,谢谢。

-- 如果我的答案是正确的,请不要忘记“标记为答案”。

To convert your sent string to the equivalent DateTime value, use the following code:

var keyDateTime = Convert.ToDateTime(strKeyTime);
var strTheTime = DateTime.Now

from here, you can use this value to compare with your original time value as the following:

if (keyDateTime == strTheTime || (keyDateTime > strTheTime && keyDateTime < strTheTime.AddMinutes(5))
{
    return true;
}

the previous block of code will first check if we got an exact match, or the time sent to the method is between the original time and a time shift of additional 5 minutes.

that's it, if this is not what you need, let me know so I may update my answer for you, thanks.

-- if my answer is correct, don't forget to "Mark as answer".

汐鸠 2024-12-20 02:26:51

“也许先将密钥(原始时间)转换回日期,然后再执行”听起来像是一个合理的解决方案。我会这样做:

如果没有有关字符串格式化方式的信息,我无法为第一步提供任何建议。然而,一旦完成了这一点,剩下的事情就很容易了。

"perhaps convert key(original time) back to a date first and then do it" sounds like a sound solution. I'd do it this way:

The first step is something I can't really give you any advice on without information concerning how your string is formatted. However, once this is done, the rest should be easy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文