我正在尝试比较来自传入请求的时间戳和数据库存储值。当然,SQL Server在时间上保持一些毫秒的精度,当读取到. net DateTime时,它包含了这些毫秒。但是,传入系统的请求不能提供这样的精度,所以我只需要去掉毫秒。

我觉得我遗漏了一些明显的东西,但我还没有找到一种优雅的方法来做到这一点(c#)。


当前回答

与其放弃毫秒然后比较,为什么不比较差异呢?

DateTime x; DateTime y;
bool areEqual = (x-y).TotalSeconds == 0;

or

TimeSpan precision = TimeSpan.FromSeconds(1);
bool areEqual = (x-y).Duration() < precision;

其他回答

四舍五入:四舍五入到第二:

dateTime.AddTicks(-dateTime.Ticks % TimeSpan.TicksPerSecond)

替换为TicksPerMinute以四舍五入到分钟。


如果您的代码对性能敏感,请谨慎处理

new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second)

我的应用在System.DateTime.GetDatePart中花费了12%的CPU时间。

DateTime d = DateTime.Now;
d = d.AddMilliseconds(-d.Millisecond);

2上述解决方案的扩展方法

    public static bool LiesAfterIgnoringMilliseconds(this DateTime theDate, DateTime compareDate, DateTimeKind kind)
    {
        DateTime thisDate = new DateTime(theDate.Year, theDate.Month, theDate.Day, theDate.Hour, theDate.Minute, theDate.Second, kind);
        compareDate = new DateTime(compareDate.Year, compareDate.Month, compareDate.Day, compareDate.Hour, compareDate.Minute, compareDate.Second, kind);

        return thisDate > compareDate;
    }


    public static bool LiesAfterOrEqualsIgnoringMilliseconds(this DateTime theDate, DateTime compareDate, DateTimeKind kind)
    {
        DateTime thisDate = new DateTime(theDate.Year, theDate.Month, theDate.Day, theDate.Hour, theDate.Minute, theDate.Second, kind);
        compareDate = new DateTime(compareDate.Year, compareDate.Month, compareDate.Day, compareDate.Hour, compareDate.Minute, compareDate.Second, kind);

        return thisDate >= compareDate;
    }

用法:

bool liesAfter = myObject.DateProperty.LiesAfterOrEqualsIgnoringMilliseconds(startDateTime, DateTimeKind.Utc);

下面是一个基于之前答案的扩展方法,它可以让你截断到任何分辨率…

用法:

DateTime myDateSansMilliseconds = myDate.Truncate(TimeSpan.TicksPerSecond);
DateTime myDateSansSeconds = myDate.Truncate(TimeSpan.TicksPerMinute)

类:

public static class DateTimeUtils
{
    /// <summary>
    /// <para>Truncates a DateTime to a specified resolution.</para>
    /// <para>A convenient source for resolution is TimeSpan.TicksPerXXXX constants.</para>
    /// </summary>
    /// <param name="date">The DateTime object to truncate</param>
    /// <param name="resolution">e.g. to round to nearest second, TimeSpan.TicksPerSecond</param>
    /// <returns>Truncated DateTime</returns>
    public static DateTime Truncate(this DateTime date, long resolution)
    {
        return new DateTime(date.Ticks - (date.Ticks % resolution), date.Kind);
    }
}

这是我的版本的扩展方法张贴在这里和类似的问题。这将以一种易于读取的方式验证tick值,并保留原始DateTime实例的DateTimeKind。(当存储到像MongoDB这样的数据库时,这有微妙但相关的副作用。)

如果真正的目标是将DateTime截断为指定的值(即小时/分钟/秒/MS),我建议在您的代码中实现此扩展方法。它确保你只能截断到一个有效的精度,它保留了重要的DateTimeKind元数据的原始实例:

public static DateTime Truncate(this DateTime dateTime, long ticks)
{
    bool isValid = ticks == TimeSpan.TicksPerDay 
        || ticks == TimeSpan.TicksPerHour 
        || ticks == TimeSpan.TicksPerMinute 
        || ticks == TimeSpan.TicksPerSecond 
        || ticks == TimeSpan.TicksPerMillisecond;

    // https://stackoverflow.com/questions/21704604/have-datetime-now-return-to-the-nearest-second
    return isValid 
        ? DateTime.SpecifyKind(
            new DateTime(
                dateTime.Ticks - (dateTime.Ticks % ticks)
            ),
            dateTime.Kind
        )
        : throw new ArgumentException("Invalid ticks value given. Only TimeSpan tick values are allowed.");
}

然后你可以使用这样的方法:

DateTime dateTime = DateTime.UtcNow.Truncate(TimeSpan.TicksPerMillisecond);

dateTime.Kind => DateTimeKind.Utc