相关疑难解决方法(0)

舍入DateTime对象

我想将日期/时间舍入到图表应用程序的最近区间.我想要一个像下面这样的扩展方法签名,以便可以在任何精度级别上实现舍入:

static DateTime Round(this DateTime date, TimeSpan span);
Run Code Online (Sandbox Code Playgroud)

我的想法是,如果我通过十分钟的时间跨度,它将四舍五入到最接近的十分钟间隔.我不能理解我的实施,并希望你们中的一个人之前会写过或使用类似的东西.

我认为楼层,天花板或最近的实施都可以.

有任何想法吗?

编辑:感谢@tvanfosson和@ShuggyCoUk,实现如下:

public static class DateExtensions {
    public static DateTime Round(this DateTime date, TimeSpan span) {
        long ticks = (date.Ticks + (span.Ticks / 2) + 1)/ span.Ticks;
        return new DateTime(ticks * span.Ticks);
    }
    public static DateTime Floor(this DateTime date, TimeSpan span) {
        long ticks = (date.Ticks / span.Ticks);
        return new DateTime(ticks * span.Ticks);
    }
    public static DateTime Ceil(this DateTime date, TimeSpan span) {
        long ticks = (date.Ticks + span.Ticks - …
Run Code Online (Sandbox Code Playgroud)

c# algorithm

100
推荐指数
3
解决办法
3万
查看次数

在SqlServer中插入的DateTime更改值

当我插入这些值

09/30/2013 05:04:56.599 
09/30/2013 05:04:56.599
09/30/2013 05:04:56.599
09/30/2013 05:04:57.082
Run Code Online (Sandbox Code Playgroud)

在SqlServer数据库中,毫秒值以奇怪的方式变化

2013-09-30 05:04:56.600 
2013-09-30 05:04:56.600 
2013-09-30 05:04:56.600 
2013-09-30 05:04:57.083
Run Code Online (Sandbox Code Playgroud)

怎么了 ?

编辑:相关代码:

        com = new SqlCommand();
        com.Connection = con;
        com.CommandText = @"INSERT INTO [AuthSourceTimings]
                                   ([FileName]
                                   ,[JobID]
                                   ,[JobCreationTime]
                                   ,[JobSendTime]
                                   ,[JobAckTime]
                                   ,[JobDoneTime])
                             VALUES
                                   (@FileName
                                   ,@JobID
                                   ,@JobCreationTime
                                   ,@JobSendTime
                                   ,@JobAckTime
                                   ,@JobDoneTime)
                            ";

        com.Parameters.AddWithValue("@FileName", fileName);
        com.Parameters.AddWithValue("@JobID", t.JobID);

        com.Parameters.AddWithValue("@JobCreationTime", t.JobCreationTime == DateTime.MinValue ? (object)DBNull.Value : (object)t.JobCreationTime);
        com.Parameters.AddWithValue("@JobSendTime", t.JobSendTime == DateTime.MinValue ? (object)DBNull.Value : (object)t.JobSendTime);
        com.Parameters.AddWithValue("@JobAckTime", t.JobAcknowledgementTime == DateTime.MinValue ? (object)DBNull.Value : (object)t.JobAcknowledgementTime);
        com.Parameters.AddWithValue("@JobDoneTime", t.JobCompletionTime == DateTime.MinValue ? …
Run Code Online (Sandbox Code Playgroud)

c# sql-server datetime

7
推荐指数
2
解决办法
305
查看次数

EF DateTimes与SQL Server中保存的值不匹配

我使用C#Entity Framework保存日期时间,当我从数据库加载该时间时,时间与我保存的值相差1毫秒或更多.

这是C#代码:

    public List<DateTime> TestDate()
    {
        var dates = new List<DateTime>();
        DateTime testvalue = DateTime.Now;
        dates.Add(testvalue);
        IactexGMG2Entities firstContext = new IactexGMG2Entities();
        var firstQuery = from p in firstContext.LocationProperties
                         where p.locationPropertyId == 4
                         select p;
        var firstRec = firstQuery.Single();
        firstRec.locationPropertyDateTime = testvalue;
        firstContext.SaveChanges();
        firstContext.Dispose();
         IactexGMG2Entities secondContext = new IactexGMG2Entities();
         var secondQuery = from p in secondContext.LocationProperties
                          where p.locationPropertyId == 4
                          select p;
        var secondRec = secondQuery.Single();

        var secondDate = secondRec.locationPropertyDateTime ?? DateTime.Now;
        dates.Add(secondDate);
        secondContext.Dispose();
        return dates;
    }
Run Code Online (Sandbox Code Playgroud)

以下是收到的值:

5/29/2015 5:43:25 …
Run Code Online (Sandbox Code Playgroud)

c# sql-server datetime entity-framework

3
推荐指数
1
解决办法
961
查看次数

标签 统计

c# ×3

datetime ×2

sql-server ×2

algorithm ×1

entity-framework ×1