我正在編寫一些數據分析軟件,我想升級原始數據的時基。我的原始數據有2分鐘的時間步長。我想按5分鐘,每小時,每天和每月的時間步長將數據縮放到多個數據庫表中。我計劃從原始數據中運行這些數據以保持精確度。如何將Vb.net中的DateTime舍入爲最接近的5分
我目前遇到的問題是取初始值並找到最接近的'圓'時間點,我想要它,成爲我的起點。例如,我將從13/03/12 00:01:36開始,作爲我的起點,我希望代碼將13/03/12 00:00:00作爲最接近的時間點,以便開始計算從那裏。對於每一個時間點,我都希望在每一方面佔用一半的時間。所以12/03/12 23:57:30到13/03/12 00:02:29將變成13/03/12 00:00:00。
使用SQL查詢從Access獲取數據,並將Date和Value存儲在兩個並排數組中。以下是我的代碼到目前爲止。它將數值四捨五入到下一個5分鐘,而不是上升或下降到最近的5分鐘。
Private Sub RateStateScale(ByVal Parameter As Integer, ByVal Timebase As String)
Dim NewDate(0)
Dim NewData(0)
Dim RecordCounter
Dim MinValue As Date = ScaleDate(0)
Dim startpoint As String
For RecordCounter = 0 To ScaleDate.GetLength(0)
If MinValue > ScaleDate(RecordCounter) Then
MinValue = ScaleDate(RecordCounter)
End If
Next
Do Until MinValue.Minute Mod 5 = 0
MinValue = MinValue.AddMinutes(1)
Loop
End Sub
感謝您的幫助
第一個數據點可能不在12/03/12 23:57:30,數據將來自幾個不同的記錄站點,在不同的站點啓動和停止倍。起點必須從數組中取出,因此循環尋找「最小值」。代碼中的註釋也表示「四捨五入到前五分鐘」,我希望代碼四捨五入到最接近的五分鐘。 – Pezzzz
@Pezzzz:請簡單地嘗試一下。如果您只看到第一條評論,則沒有閱讀整個代碼。 '12/03/12 23:57:30'將四捨五入到12/03/13 00:00:00。沒有必要使用陣列中的任何內容,因爲您只需要四捨五入到最近的五分鐘。 –
@Blam,當我第一次查看代碼時,它與現在是不同的,有3行以var,註釋和if語句開頭。他在編輯中改變了它。 – Pezzzz