0

I have created a WEB API in which I am sending a serial number and a date time. In result it gives me the last/latest record value.

What I have done

Below is my controller code

 var dateTime = dt.ToString(); // dt is the user sent date time
 var result = medEntitites.tj_xhqd.Where(m => m.zdjh == msn) // msn is the serial number 
                                             .OrderByDescending(o => o.sjsj)
                                             .Select(s => s.sjsj)//sjsj is datetime in DB
                                             .First().ToString();  

I am testing the API via Postman. Date time sent by me is in T format like 2017-10-22T13:30:20and in controller the date time is 10/22/2017 1:30:20 PM

In response result the date time getting the last/latest record which is 10/21/2017 6:15:08 PM

API URL: http://localhost:14909/api/meters/GetByMsn/002999000536/2017-10-22T13:30:20

What I want to do?

Now I want to compare the user sent date time with resulted date time in such a way the resulted date time is in within the 10 minutes time span of user sent date time considering the date is same i.e. If the resulted date time is within the 10 minutes time span of the current date time then it will send a YES response other wise NO

What I know?

I know how to add or subtract minutes and how to compare two date times, also I know how to set a response on the basis of result.

The problem I am facing is to check with the time span

Any help would be highly appreciated.

Moeez
  • 494
  • 9
  • 55
  • 147

1 Answers1

1

Here's a simple way to check the time difference in minutes.

DateTime userSent = new DateTime(2017, 10, 22, 13, 20, 20);
DateTime result = new DateTime(2017, 10, 22, 18, 15, 08);

double diffInMinutes = Math.Abs((result - userSent).TotalMinutes); // can be negative

if(diffInMinutes <= 10)
    return "Yes";
else
    return "No";
jegtugado
  • 5,081
  • 1
  • 12
  • 35
  • I am passing both `dt` and `result` in `new DateTime(dt)` and `new DateTime(result)` but it's not allowing me to do it. – Moeez Oct 22 '17 at 10:21