【问题标题】:ASP.net dateADD helpASP.net dateADD 帮助
【发布时间】:2011-04-21 21:31:37
【问题描述】:

大家好,我有以下代码可以将下拉列表填充到公司 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)

大卫

【问题讨论】:

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


    【解决方案1】:

    我会更改很多内容(包括使用 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
    

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

    【讨论】:

    • 这也符合我的需要!代码比我少!谢谢本! :o)
    【解决方案2】:

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

    当dtTempNow > dtTempEnd 不再为 True 时它会停止。另外,您可能对 String.Format 感兴趣

    应该是这样开始的:

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

    为什么?你一开始就设置nTemp为1,为什么要从13开始?

    【讨论】:

      【解决方案3】:

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

      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 进行排序,然后将列表绑定到下拉列表。

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-31
        • 2011-08-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多