-3

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));
aaron
  • 68
  • 6
  • 2
    Does `8/27/2022 3:40:26 PM` look like an integer (a whole number) to you? It certainly doesn't to me. Stop working with `Ticks`. It doesn't make any sense in the majority of scenarios. – ProgrammingLlama Aug 30 '22 at 01:05
  • Also, if it's _your_ API that is returning a date and time value as `M/d/yyyy h:mm:ss tt` format, I'd suggest you modify your API to use something more standardised like the [round-trip date/time pattern `"o"`](https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings) (also known as ISO8601 or an ISO date string). – ProgrammingLlama Aug 30 '22 at 01:09
  • 1
    Note that your lines `long elapsedTicks = currentDate.Ticks - centuryBegin.Ticks; TimeSpan elapsedSpan = new TimeSpan(elapsedTicks);` should be written as `TimeSpan elapsedSpan = currentDate - centuryBegin;`. – ProgrammingLlama Aug 30 '22 at 01:10
  • 1
    Somewhat unrelated note: it is quite ridiculous to call `.ToString` on a string... You may want to re-read [mre] guidance to make sure to show all necessary details - i.e. there is no way to know what type `adding` variable is because you for some reason decided to hide the type from the readers of the question by using `var` - try to avoid hiding such important information in your future questions. Also the original title had nothing to do with with problem shown - updating tile after you done writing the post may be good idea (I did that for this post as an example). – Alexei Levenkov Aug 30 '22 at 01:19
  • i was using ticks as i need to compare two times, as my title suggested, to do this i was going to subtract the milliseconds from each other. – aaron Aug 30 '22 at 04:01
  • I think you should do something like this: `var prevDate = new DateTime(2022, 8, 30, 11, 25, 26); var now = DateTime.Now; TimeSpan delta = now - prevDate; Console.WriteLine(delta); Console.WriteLine(delta > TimeSpan.FromMinutes(15));` – keuleJ Aug 30 '22 at 12:37
  • my issue with var prevDate = new DateTime(2022, 8, 30, 11, 25, 26); var now = DateTime.Now; TimeSpan delta = now - prevDate; Console.WriteLine(delta); Console.WriteLine(delta > TimeSpan.FromMinutes(15)); is that i am not sure how to take M/d/yyyy h:mm:ss tt and make it work with long – aaron Aug 30 '22 at 13:21
  • @aaron why do you need to work with longs? You can subtract two dates directly and get a `timeSpan` I think you're hung up on the error that the `new DateTime()` requires a `long` input, but _you don;t need to create a new data at all_. – D Stanley Aug 30 '22 at 14:16

1 Answers1

2

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
}
D Stanley
  • 149,601
  • 11
  • 178
  • 240