2016-06-14 75 views
-1

虽然我知道如何做到这一点,但在我的脑海中,我想确保我正在充分利用执行任务的语法。
我期待取一个日期,并添加35天的增量,直到它在未来达到一个日期。
有没有一种更好的方式在VB.NET中做到这一点?增加日期35天,并将结果存储在数组中

'\* define the end of the repeat period (where dteValuationDate is, say, 01/01/2016)... 
dteReminderEndDate = DateAdd(DateInterval.Month, 15, dteValuationDate) 
'\* define the start date for the loop (which is a specific date, say, 17/05/2016, and add 70 days to it)... 
dteLoopStart = DateAdd(DateInterval.Day, 70, dteStartDate)   
intDateCount = 1    
While dteRollingDate < dteReminderEndDate 
    '\* calculate the actual unadjusted rolling date... 
    dteRollingDate = DateAdd(DateInterval.Day, (intDateCount * 35), dteLopStart)     
    _dteReminderDates(intDateCount) = dteRollingDate    
    intDateCount = intDateCount + 1     
    ReDim Preserve _dteReminderDates(intDateCount) 
End While 

所以我只想存储结束日期之前的日期,即01/01/2016 + 15个月。虽然我很舒服,我正在做的事情基本上是'正确的',但我很想知道是否有更好的方法来做到这一点?

+0

请停止使用行由行留言,让我们为自己的代码说话 –

回答

3

我觉得这其实是你正在寻找:

Dim startDate As Date = Convert.ToDateTime("01/01/2016") 
Dim endDate As Date = Convert.ToDateTime("17/05/2016") 
Dim PossibleDateList As List(Of Date) = New List(Of Date) 
While startDate <= endDate 
    PossibleDateList.Add(startDate) 
    startDate = startDate.AddDays(35) 
End While 

最后PossibleDateList将包含所有必需的日期

+0

很整洁方法,没有考虑到这一点。但我刚刚被要求考虑日期数组是二维的,因为我需要存储几个记录的日期(有35条记录,但现在三个不是一个的初始请求,具有日期数组要求,尽管这个数字未来可能会增加)。我认为我原来的做法是为了迎合这种情况而设计的。所以这个数组就像_dteReminderDates(a,intDateCount)= dteRollingDate – MartinS