Home > Back-end >  time difference between datetime value SQLite C#
time difference between datetime value SQLite C#

Time:11-12

I have a DateTime record in SQLite database.

time

11-08-2021 15:16:44
11-08-2021 17:09:22
11-09-2021 17:20:39
...
11-11-2021 09:31:54
11-11-2021 10:35:37
11-11-2021 10:45:11
11-11-2021 11:54:28

I filter the DateTime that fall in the specific date range by using

string date = DateTime.Now.AddHours(-3).ToString("MM-dd-yyyy HH:mm:00Z");
string query = $"SELECT * FROM LastRun WHERE LastUpdateTime > '{queryDate}' ";

I able to get the results as below.

time

11-11-2021 09:31:54
11-11-2021 10:35:37
11-11-2021 10:45:11
11-11-2021 11:54:28

The next thing is I want to get the time difference among each other like below.

time                  difference (hour)

 11-11-2021 09:31:54   1
 11-11-2021 10:35:37   ??
 11-11-2021 10:45:11   ??
 11-11-2021 11:54:28   ??

Is it possible to get the time difference like this and how I query and get the difference of the time? Any suggestion?

CodePudding user response:

One way is to do this in the c# side:

DateTime? prev = null;

while(reader.Read()){
  DateTime thisDateTime = Convert.ToDateTime(reader[0]); 

  if(prev != null)
    Console.WriteLine("time diff is "   prev.Value - thisDateTime );


  prev = thisDateTime;
}

Prev will always lag behind the current date

  • Related