2016-06-14 73 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