简体   繁体   English

如何将Vb.net中的DateTime舍入到最接近的5分钟

[英]How to Round DateTime in Vb.net to the nearest 5mins

I am writing some data analysis software, I want to upscale timebase of my Raw Data. 我正在编写一些数据分析软件,我想扩展原始数据的时基。 My Raw Data has a time step of ~2minutes. 我的原始数据的时间步长约为2分钟。 I want to scale the data into several database tables with a timestep of 5minutes, hourly, daily and monthly. 我想将数据按小时,每小时,每天和每月5分钟的比例扩展到几个数据库表中。 I plan to run each of these from the raw data to keep my accuracy up. 我计划从原始数据中运行这些数据,以保持准确性。

The problem I am currently having is taking an initial value and finding the closest 'round' time point I want to it, to be my start point. 我当前遇到的问题是获取一个初始值,并找到我想要的最接近的“舍入”时间点作为我的起点。 For example, I will start with the point 13/03/12 00:01:36 as my start point, I want the code to find 13/03/12 00:00:00 as the closest time point so it will start calculating from there. 例如,我将从点13/03/12 00:01:36开始作为我的起点,我希望代码找到13/03/12 00:00:00作为最接近的时间点,因此它将开始计算从那里。 For each time point I want to take half of the time step on each side. 对于每个时间点,我要在每一侧花费一半的时间。 So 12/03/12 23:57:30 to 13/03/12 00:02:29 will become 13/03/12 00:00:00. 因此12/03/12 23:57:30至13/03/12 00:02:29将变为13/03/12 00:00:00。

The Data is taken from Access using a SQL query and the Date and Value are stored in two side by side Arrays. 使用SQL查询从Access中获取数据,并将日期和值存储在两个并排的数组中。 Below is my code so far. 下面是到目前为止的代码。 It will round the values up to the NEXT 5 minutes, rather than up or down to the NEAREST 5 mimutes. 它将把值向上舍入到下一个5分钟,而不是向上或向下舍入到NEAREST 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

Thanks for your help 谢谢你的帮助

Let's try some VB, for a "round to nearest 5 minutes" function: 让我们尝试一些VB,以获得“四舍五入到五分钟”的功能:

' just some date, should be a parameter to your function
Dim mydatetime = new DateTime(2012,3,12,23,57,30)

' split into date + time parts
Dim datepart = mydatetime.Date
Dim timepart = mydatetime.TimeOfDay

' round time to the nearest 5 minutes
timepart = TimeSpan.FromMinutes(Math.Floor((timepart.TotalMinutes+2.5)/5.0) * 5.0)

' combine the parts
Dim newtime = datepart.Add(timepart)
' a function would return this value

One possibility would be the following: 一种可能是以下情况:

var date = new DateTime(2012,03,12,23,57,30);
var fullFiveMinutes = date.Minute / 5;
// result will be our date rounded down to the previous full five minutes
var result = new DateTime(date.Year, date.Month, date.Day
                          date.Hour, fullFiveMinutes * 5, 0);

// if we add exactly 2.5 minutes to our date variable and the result represents
// another full five minutes we need to round up.
if(date.AddSeconds(2.5 * 60).Minute / 5 != fullFiveMinutes)
    result = result.AddMinutes(5);

This is C# code, I am sure you can translate it. 这是C#代码,我相信您可以翻译它。

Could you do? 可以吗 (very basic, but it should give an idea) (非常基本,但是应该给出一个想法)

Dim tValue As Date = ScaleDate(0) 

'find the next highest 5 minute mark
For RecordCounter = 0 To ScaleDate.GetLength(0)              
    If tValue > ScaleDate(RecordCounter) Then                  
        tValue = ScaleDate(RecordCounter)              
    End If          
Next  

Do Until tValue.Minute Mod 5 = 0         
        tValue = tValue.AddMinutes(1)     
Loop

'compare the original value to the next highest.  If more than 2.5 minutes, then subtract 5 minutes
If DateDiff(DateInterval.Second, tValue, MinValue) > 150 Then
    MinValue = tValue.AddMinutes(-5)
Else
    MinValue = tValue
End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM