开发者

ASP.net dateADD help

Hey all, i have the following code to populate a dropdown to coinside with a companies 2008-2010 calander year. This is the output:

 2008 Period 1: 11/30/2008 - 12/27/2008
 2008 Period 2: 12/28/2008 - 1/24/2009
 2009 Period 3: 1/25/2009 - 2/21/2009
 2009 Period 4: 2/22/2009 - 3/21/2009
 2009 Period 5: 3/22/2009 - 4/18/2009
 2009 Period 6: 4/19/2009 - 5/16/2009
 2009 Period 7: 5/17/2009 - 6/13/2009
 2009 Period 8: 6/14/2009 - 7/11/2009
 2009 Period 9: 7/12/2009 - 8/8/2009
 2009 Period 10: 8/9/2009 - 9/5/2009
 2009 Period 11: 9/6/2009 - 10/3/2009
 2009 Period 12: 10/4/2009 - 10/31/2009
 2009 Period 13: 11/1/2009 - 11/28/2009
 2009 Period 1: 11/29/2009 - 12/26/2009
 2009 Period 2: 12/27/2009 - 1/23/2010
 2010 Period 3: 1/24/2010 - 2/20/2010
 2010 Period 4: 2/21/2010 - 3/20/2010
 2010 Period 5: 3/21/2010 - 4/17/2010
 2010 Period 6: 4/18/2010 - 5/15/2010
 2010 Period 7: 5/16/2010 - 6/12/2010
 2010 Period 8: 6/13/2010 - 7/10/2010
 2010 Period 9: 7/11/2010 - 8/7/2010
 2010 Period 10: 8/8/2010 - 9/4/2010
 2010 Period 11: 9/5/2010 - 10/2/2010

and here is the code for it:

    Dim dt2009Start As DateTime
    Dim dtTempStart, dtTempEnd As DateTime
    Dim dtTempNow As DateTime
    Dim nTemp As Integer
    Dim itemPeriod As ListItem
    Dim timesAround As Integer

    dt2009Start = Convert.ToDateTime("11/30/2008")
    dtTempStart = dt2009Start
    dtTempEnd = dtTempStart.AddDays(27)

    ddlInvoicePeriods.Items.Clear()

    dtTempNow = DateTime.Now()
    nTemp = 1
    timesAround = 0

    While (dtTempNow > dtTempEnd)
        If nTemp = 12 Then
            If timesAround = 0 Then
                'dtTempStart = Convert.ToDateTime("10/25/2009")
                'dtTempEnd = Convert.ToDateTime("11/18/2009")
            End If
        ElseIf nTemp = 14 Then
            If timesAround = 0 Then
                dtTempStart = Convert.ToDateTime("11/29/2009")
                dtTempEnd = Convert.ToDateTime("12/26/2009")
                nTemp = 1
                timesAround += 1
            End If
        End If

        itemPeriod = New ListItem()
        itemPeriod.Text = dtTempStart.Date.Year.ToString() & " Period " & nTemp.ToString() & ": " & dtTempStart.Date.ToShortDateString() & " - " & dtTempEnd.Date.ToShortDateString()
        itemPeriod.Value = dtTempStart.Date.ToShortDateString() & "-" & dtTempEnd.Date.ToShortDateString()
        ddlInvoicePeriods.Items.Add(itemPeriod)
        itemPeriod = Nothing

        Debug.Print(dtTempStart.Date.Year.ToString() & " Period " & nTemp.ToString() & ": " & dtTempStart.Date.ToShortDateString() & " - " & dtTempEnd.Date.ToShortDateString())

        dtTempStart = dtTempEnd.AddDays(1)
        dtTempEnd = dtTempStart.AddDays(27)
        nTemp += 1
    End While

    If nTemp = 12 Then
        dtTempStart = Convert.ToDateTime("12/27/2009")
        dtTempEnd = Convert.ToDateTime("11/18/2009")
    ElseIf nTemp = 13 Then
        dtTempStart = Convert.ToDateTime("11/19/2009")
        dtTempEnd = Convert.ToDateTime("12/16/2009")
    End If

    itemPeriod = New ListItem
    itemPeriod.Text = dtTempStart.Date.Year.ToString()开发者_StackOverflow中文版 & " Period " & nTemp.ToString() & ": " & dtTempStart.Date.ToShortDateString() & " - " & dtTempEnd.Date.ToShortDateString()
    itemPeriod.Value = dtTempStart.Date.ToShortDateString() & "-" & dtTempEnd.Date.ToShortDateString()
    ddlInvoicePeriods.Items.Add(itemPeriod)
    itemPeriod = Nothing

As you may have noticed, the output only goes to 2010 (10/2/2010) which should go all the way to 12/31/2011.

Problem being:

It should start out like so:

2008 Period 13: Nov 30 - Dec 27
2009 Period  1: Dec 28 - Jan 24
2009 Period  2: Jan 25 - Feb 21
2009 Period  3: Feb 22 - Mar 21
...
2009 Period 13: Nov 29 - Dec 26
2010 Period  1: Dec 27 - Jan 23
2010 Period  2: Jan 24 - Feb 20
..
2010 Period 13: Nov 28 - Dec 25
2011 Period  1: Dec 26 - Jan 22
2011 Period  2: Jan 23 - Feb 19
...etc

Any help would be awesome as i am at my end of trying to figure out what i am doing wrong! :o)

David


There is a lot I would change (including using Iain's class for representing the period), and recognizing that nTemp needs to start at 13 for this example and the list is stopping because the loop was told to break at DateTime.Now for no apparent reason, eventhough you said it should continue to run to the end of 2011. But here is how to make the code you posted work:

Dim dt2009Start As DateTime
    Dim dtTempStart, dtTempEnd As DateTime
    Dim dtTempNow As DateTime
    Dim nTemp As Integer
    Dim itemPeriod As ListItem
    Dim timesAround As Integer
    Dim dtReallyEnd As DateTime
    Dim year As Integer

    year = 2008
    dt2009Start = Convert.ToDateTime("11/30/2008")
    dtTempStart = dt2009Start
    dtTempEnd = dtTempStart.AddDays(27)
    dtReallyEnd = Convert.ToDateTime("12/31/2011")


    dtTempNow = DateTime.Now()
    nTemp = 13
    timesAround = 0

    While (dtReallyEnd > dtTempEnd)
       If nTemp = 14 Then
           nTemp = 1
            timesAround += 1
            year += 1
        End If


        itemPeriod = New ListItem()
        itemPeriod.text = year.ToString() & " Period " & nTemp.ToString() & ": " & dtTempStart.Date.ToShortDateString() & " - " & dtTempEnd.Date.ToShortDateString()
        itemPeriod.value = dtTempStart.Date.ToShortDateString() & "-" & dtTempEnd.Date.ToShortDateString()
        ddlInvoicePeriods.Items.Add(itemPeriod)
        itemPeriod = Nothing

        Debug.Print(dtTempStart.Date.Year.ToString() & " Period " & nTemp.ToString() & ": " & dtTempStart.Date.ToShortDateString() & " - " & dtTempEnd.Date.ToShortDateString())

        dtTempStart = dtTempEnd.AddDays(1)
        dtTempEnd = dtTempStart.AddDays(27)
        nTemp += 1
    End While

    If nTemp = 14 Then
        nTemp = 1
        timesAround += 1
        year += 1
    End If

    itemPeriod = New ListItem
    itemPeriod.text = dtTempStart.Date.Year.ToString() & " Period " & nTemp.ToString() & ": " & dtTempStart.Date.ToShortDateString() & " - " & dtTempEnd.Date.ToShortDateString()
    itemPeriod.value = dtTempStart.Date.ToShortDateString() & "-" & dtTempEnd.Date.ToShortDateString()
    ddlInvoicePeriods.Items.Add(itemPeriod)
    itemPeriod = Nothing

I would still probably make the end date more dynamic (so you don't have to change code in the future when you want more Periods to show) and the start date might be dynamic as well (to keep the list at a manageable length).


As you may have noticed, the output only goes to 2010 (10/2/2010) which should go all the way to 12/31/2011.

It stops when dtTempNow > dtTempEnd is no longer True. Plus, you might be interesting in String.Format

It should start out like so:

2008 Period 13: Nov 30 - Dec 27

Why? You set nTemp to 1 at the start, why should it start at 13?


I would create an object to hold your data, similar to this

Public Class Period
    Public Property Period As Integer
    Public Property StartOfPeriod As Date
    Public Property EndOfPeriod As Date
End Class

Then for each iteration of the While loop I would enter the values as list(of Period), once your code has process, use a linq query to sort the data use Period and Date, then bind the list to the drop down.

Hope this helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜