這個問題是從超級用戶遷移過來的,因為可以在 Stack Overflow 上回答。 7 天前遷移 。
我在 SQLite 資料庫中有一個 DateTime 記錄。
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
我通過使用過濾掉在特定日期范圍內的日期時間
string date = DateTime.Now.AddHours(-3).ToString("MM-dd-yyyy HH:mm:00Z");
string query = $"SELECT * FROM LastRun WHERE LastUpdateTime > '{queryDate}' ";
我能夠得到如下結果。
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
接下來是我想得到彼此之間的時差,如下所示。
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 ??
是否有可能獲得這樣的時差以及我如何查詢和獲得時差?有什么建議嗎?
編輯:
Now I have a fix time span, which is '00:10:00'. I want to make a compare between time difference that get before and this timespan. However, I get the wrong result since the value of '(thisDateTime - prev.Value)' becomes '00:00:00" when I try to insert the value. Please look at this pic (Result). So, all the result that return becomes '1'.
while(reader.Read())
{
......
prev = thisDateTime;
TimeSpan fixTimeDiff = new TimeSpan(0, 10, 0);
//Console.WriteLine(fixTimeDiff);
var d = thisDateTime - prev.Value;
int compare = TimeSpan.Compare(fixTimeDiff, d);
Console.WriteLine("{0} {1} {2} ( Compare should return {3} )",
fixTimeDiff, compare == 1 ? ">" : compare == 0 ? "=" : "<", d,
compare);
}
uj5u.com熱心網友回復:
一種方法是做到這一點的C#的一面:
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 將始終落后于當前日期
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/360110.html
