2

在我的服务端点中,我得到了一个表示 GMT 时间的字符串。

我必须验证通过的时间在当前时间的 5 分钟内。

我应该怎么做?

        bool result = false;
        DateTime gmt;
        if (DateTime.TryParse(passedInGmtAsString, out gmt))
        {
            DateTime utcNow = DateTime.UtcNow;

            result  = ??
        }
4

5 回答 5

5

在您将传入的日期解析为适当的格式后,您可以使用该DateTime.Subtract方法来确定差异,例如

var minutes = utcNow.Subtract(gmt).TotalMinutes
于 2013-06-12T17:49:21.170 回答
2

最直接的解决方案似乎是

result = Math.Abs((gmt - utcNow).TotalMinutes) < 5;
于 2013-06-12T17:52:41.627 回答
1
Result = ((DateTime.UtcNow - gmt).TotalMinutes <= 5)
于 2013-06-12T17:54:16.400 回答
0

试试这个:

    bool result = false;
    DateTime gmt;
    if (DateTime.TryParse(passedInGmtAsString, out gmt))
    {
        DateTime utcNow = DateTime.UtcNow;

        result  = (gmt - utcNow).TotalMinutes <= 5;
    }
于 2013-06-12T17:49:48.857 回答
0

您可以通过以下方式获得结果

result = TimeSpan.Compare(gmt.Subtract(utcNow), TimeSpan.FromMinutes(5)) == -1;

-1如果第一个时间跨度(差异)短于第二个时间跨度(5 分钟),则比较将返回。

于 2013-06-12T17:49:51.233 回答