Im trying to accurately subtract two datetime, but I am having difficulty.
My end goal is to see if 15 minutes or more have passed from record.DateCreated. if so, run some code to delete.
Console.WriteLine(record.DateCreated);
//8/27/2022 3:40:26 PM
Console.WriteLine(record.DateCreated.GetType());
//System.DateTime
I found
var prevDate = new DateTime(2022, 8, 27,3,40,26);
var today = DateTime.Now;
Console.WriteLine("prevDate: {0}", prevDate);
Console.WriteLine("today: {0}", today);
//get difference of two dates
var diffOfDates = today - prevDate;
Console.WriteLine("Difference in Timespan: {0}", diffOfDates);
Console.WriteLine("Difference in Days: {0}", diffOfDates.Days);
Console.WriteLine("Difference in Hours: {0}", diffOfDates.Hours);
Console.WriteLine("Difference in Miniutes: {0}", diffOfDates.Minutes);
but now when i try to write
var prevDate = new DateTime(record.DateCreated);
error is cannot convert to long.
ive tried to do as Convert DateTime to long and also the other way around
but do not understand this.
also tried
//DateTime dt = DateTime.ParseExact(LastSyncDateTime, "M/d/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);
//string result = dt.ToString("yyyy, MM, dd, h, mm, ss"); ;
im really close with what i need with
DateTime recordTime = record.DateCreated.Value;
DateTime x15MinsLater = recordTime.AddMinutes(15);
Console.WriteLine(string.Format("{0} {1}", recordTime, x15MinsLater));
CodePudding user response:
You already have DateTime
values, you don't need any conversion to or from longs, strings, or anything else. You can simply do:
var prevDate = record.DateCreated;
var today = DateTime.Now;
Console.WriteLine("prevDate: {0}", prevDate);
Console.WriteLine("today: {0}", today);
//get difference of two dates
var diffOfDates = today - prevDate;
if (diffOfDates.TotalMinutes > 15)
{
// more than 15 minutes have passed
}