找出表格和午夜之间的差异(ASP.NET MVC)

时间:2021-04-17 21:30:32

I have table with data

我有数据表

Here is Model

这是模特

public partial class Logging
{
    public string Imei { get; set; }
    public Nullable<System.DateTime> CurDateTime { get; set; }
    public Nullable<System.DateTime> GPSDateTime2 { get; set; }
    public Nullable<decimal> Latitude2 { get; set; }
    public Nullable<decimal> Longitude2 { get; set; }
    public string Speed { get; set; }
    public Nullable<int> Datatype { get; set; }
    public int Id { get; set; }
    [NotMapped]
    public TimeSpan? FirstStartDifference { get { return CurDateTime - DateTime.Today; } }
    [NotMapped]
    public int coeff = 2;
}

I need to find data between CurDateTime and midnight in minuutes

我需要在MinDute中找到CurDateTime和午夜之间的数据

But public TimeSpan? FirstStartDifference { get { return CurDateTime - DateTime.Today; } } not right because DateTime.Today is today and I get difference in -.

但公共TimeSpan? FirstStartDifference {get {return CurDateTime - DateTime.Today;不对,因为DateTime.Today现在是今天我得到了 - 。

On Controller I do it like this

在控制器上,我这样做

   public JsonResult GetStops()
    {
        using (var ctx = new GoogleMapTutorialEntities())
        {
            var firstitem = ctx.Loggings.Where(x => x.Datatype == 1).AsEnumerable().Select(
                x => new
                {
                    lng = x.Longitude2,
                    lat = x.Latitude2,
                    difference = (int)(x.FirstStartDifference?.TotalMinutes ?? -1) * x.coeff
                }).FirstOrDefault();

            return Json(firstitem, JsonRequestBehavior.AllowGet);
        }
    }

How I can calculate difference correctly

我怎样才能正确计算差异

4 个解决方案

#1


0  

So, you are subtracting your CurDateTime which is 10/18/2017 8:05:38 AM from DateTime.Today which is today's date at 00:00. This will of course end up with minus value, because CurDateTime is smaller than today's date.

所以,你从DateTime.Today减去你的CurDateTime,即10月18日上午8:05:38,这是今天00:00的日期。这当然会以负值结束,因为CurDateTime小于今天的日期。

You need to subtract it from the midnight datetime of your CurDateTime. Like this:

您需要从CurDateTime的午夜日期时间中减去它。喜欢这个:

var midnight = new DateTime(CurDateTime.Year, CurDateTime.Month, CurDateTime.Day, 00, 00, 00);
var difference = CurDateTime - midnight;
var inMin = difference.TotalMinutes;

In your case:

在你的情况下:

 [NotMapped]
 public TimeSpan? FirstStartDifference
 {
    get
    {
      if(CurDateTime != null)
      {
        var midnight = new DateTime(CurDateTime.Year, CurDateTime.Month, CurDateTime.Day, 00, 00, 00);
        var difference = CurDateTime - midnight
        return difference;
      }
      return null;
    }
 }

#2


0  

you can get the difference of two time using

你可以得到两次使用的差异

TimeSpan duration = DateTime.Parse(endTime).Subtract(DateTime.Parse(startTime));

#3


0  

You could do your calculation like you want or described by other answers but use .Duration() on result. The method Duration will return the absolut result instead of a negative one.

您可以按照自己的意愿进行计算或通过其他答案进行描述,但在结果上使用.Duration()。方法持续时间将返回绝对结果而不是负结果。

Four your line of code:

四行代码:

public TimeSpan? FirstStartDifference 
{
    get 
    {
        if (!CurDateTime.HasValue)
        {
            return null
        }

        return (CurDateTime - DateTime.Today).Duration(); 
    } 
}

If you want the date value only from a DateTime object, just use the Date property instead of creating a new one.

如果只想从DateTime对象获取日期值,只需使用Date属性而不是创建新属性。

e.g.

var myDateTime = DateTime.Now;
var myDate = myDateTime.Date;

#4


0  

You can take properties Hour, Minute and Second and construct new DateTime from these values and it will be your needed difference. Or you can create new DateTime with 0 hour, 0 min and 0 sec and make substraction.

您可以使用“小时”,“分钟”和“秒”属性,并根据这些值构建新的DateTime,这将是您所需要的差异。或者你可以用0小时,0分钟和0秒创建新的DateTime并进行减法。

Here is working example

这是工作的例子

#1


0  

So, you are subtracting your CurDateTime which is 10/18/2017 8:05:38 AM from DateTime.Today which is today's date at 00:00. This will of course end up with minus value, because CurDateTime is smaller than today's date.

所以,你从DateTime.Today减去你的CurDateTime,即10月18日上午8:05:38,这是今天00:00的日期。这当然会以负值结束,因为CurDateTime小于今天的日期。

You need to subtract it from the midnight datetime of your CurDateTime. Like this:

您需要从CurDateTime的午夜日期时间中减去它。喜欢这个:

var midnight = new DateTime(CurDateTime.Year, CurDateTime.Month, CurDateTime.Day, 00, 00, 00);
var difference = CurDateTime - midnight;
var inMin = difference.TotalMinutes;

In your case:

在你的情况下:

 [NotMapped]
 public TimeSpan? FirstStartDifference
 {
    get
    {
      if(CurDateTime != null)
      {
        var midnight = new DateTime(CurDateTime.Year, CurDateTime.Month, CurDateTime.Day, 00, 00, 00);
        var difference = CurDateTime - midnight
        return difference;
      }
      return null;
    }
 }

#2


0  

you can get the difference of two time using

你可以得到两次使用的差异

TimeSpan duration = DateTime.Parse(endTime).Subtract(DateTime.Parse(startTime));

#3


0  

You could do your calculation like you want or described by other answers but use .Duration() on result. The method Duration will return the absolut result instead of a negative one.

您可以按照自己的意愿进行计算或通过其他答案进行描述,但在结果上使用.Duration()。方法持续时间将返回绝对结果而不是负结果。

Four your line of code:

四行代码:

public TimeSpan? FirstStartDifference 
{
    get 
    {
        if (!CurDateTime.HasValue)
        {
            return null
        }

        return (CurDateTime - DateTime.Today).Duration(); 
    } 
}

If you want the date value only from a DateTime object, just use the Date property instead of creating a new one.

如果只想从DateTime对象获取日期值,只需使用Date属性而不是创建新属性。

e.g.

var myDateTime = DateTime.Now;
var myDate = myDateTime.Date;

#4


0  

You can take properties Hour, Minute and Second and construct new DateTime from these values and it will be your needed difference. Or you can create new DateTime with 0 hour, 0 min and 0 sec and make substraction.

您可以使用“小时”,“分钟”和“秒”属性,并根据这些值构建新的DateTime,这将是您所需要的差异。或者你可以用0小时,0分钟和0秒创建新的DateTime并进行减法。

Here is working example

这是工作的例子