ASP.net dateADD帮助

时间:2010-10-06 15:05:57

标签: asp.net vb.net visual-studio visual-studio-2008

嘿所有,我有以下代码填写公司2008-2010年度的一个下拉列表。这是输出:

 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

以下是代码:

    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() & " Period " & nTemp.ToString() & ": " & dtTempStart.Date.ToShortDateString() & " - " & dtTempEnd.Date.ToShortDateString()
    itemPeriod.Value = dtTempStart.Date.ToShortDateString() & "-" & dtTempEnd.Date.ToShortDateString()
    ddlInvoicePeriods.Items.Add(itemPeriod)
    itemPeriod = Nothing

您可能已经注意到,输出仅到2010年(2010年10月2日),应该一直到2011年12月31日。

问题是:

应该这样开始:

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

任何帮助都会很棒,因为我在努力弄清楚我做错了什么! :O)

大卫

3 个答案:

答案 0 :(得分:2)

有很多我会改变(包括使用Iain的类来表示句点),并且认识到nTemp需要从13开始这个例子并且列表停止因为循环被告知在DateTime.Now中断没有明显的原因,尽管你说它应该继续运行到2011年底。但是这里是如何使你发布的代码工作:

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

我仍然可能会使结束日期更具动态性(因此,当您希望显示更多期间时,您不必在将来更改代码)并且开始日期也可能是动态的(将列表保留在可管理的长度)。

答案 1 :(得分:1)

  

您可能已经注意到了输出   只到2010年(10/2/2010)   应该一直到12/31/2011。

dtTempNow > dtTempEnd不再为True时停止。另外,你可能会对String.Format

感兴趣
  

应该这样开始:

     

2008年期间13:11月30日 - 12月27日

为什么呢?你在开始时将nTemp设置为1,为什么它应该从13开始?

答案 2 :(得分:1)

我会创建一个保存数据的对象,类似于

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

然后对于While循环的每次迭代,我会输入值list(of Period),一旦你的代码有进程,使用linq查询对数据进行排序使用Period和Date,然后将列表绑定到下拉列表

希望这有帮助