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
But public TimeSpan? FirstStartDifference { get { return CurDateTime - DateTime.Today; } }
public TimeSpan? FirstStartDifference { get { return CurDateTime - DateTime.Today; } }
public TimeSpan? FirstStartDifference { get { return CurDateTime - DateTime.Today; } }
not right because DateTime.Today
is today and I get difference in -
.
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
你可以得到两个时间的差
TimeSpan duration = DateTime.Parse(endTime).Subtract(DateTime.Parse(startTime));
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.
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.
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.
eg
var myDateTime = DateTime.Now;
var myDate = myDateTime.Date;
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.
You need to subtract it from the midnight datetime of your CurDateTime. Like this:
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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.