【问题标题】:Adding Day to sepcial Date with Duplicating count of friday使用重复的星期五计数将日期添加到特殊日期
【发布时间】:2015-05-19 05:33:15
【问题描述】:

我正在尝试在 VB.net 中创建递归函数

该功能将为特殊日期添加一些日子。如果有一个 星期五在添加天的范围之间,它将计算它们并将它们添加到 再次约会。

它必须是递归的,因为如果在添加超过 7 天后还有一个星期五 它还必须再增加一天。像这样:

原始日期:2015 年 5 月 19 日
要添加的日期:30
添加日期:2015 年 6 月 18 日
2015 年 5 月 19 日至 2015 年 6 月 18 日之间的星期五计数:4

添加星期五后的新日期:2015 年 6 月 22 日
添加旧星期五计数后发生的星期五计数:1
新日期和最终结果:2015 年 6 月 23 日

我认为最后两步必须是递归的。这是我到目前为止所做的,没有最后两步:

   Public Function CountOfFriday(ByVal StartDate As Date, ByVal DayToAdd As Int32) As Int32
        Dim newDate As Date = StartDate
        Dim OriginalDate As Date = StartDate
        Dim friday_count As Integer
        For value As Integer = 1 To DayToAdd
            OriginalDate = OriginalDate.AddDays(1)
            If OriginalDate.DayOfWeek = DayOfWeek.Friday Then
                newDate = newDate.AddDays(1)
                friday_count += 1
            End If

        Next
        Return friday_count

    End Function

我将此计数的结果添加到我的旧日期。

现在我怎样才能实现最后两个步骤?

【问题讨论】:

    标签: vb.net


    【解决方案1】:

    这会返回最终日期(希望有帮助)

    Function CountOfFriday(ByVal StartDate As Date, ByVal DayToAdd As Int32) As Date
        Dim newDate As Date = StartDate
        Dim OriginalDate As Date = StartDate
        Dim friday_count As Integer = 0
        For value As Integer = 1 To DayToAdd
            OriginalDate = OriginalDate.AddDays(1)
            If OriginalDate.DayOfWeek = DayOfWeek.Friday Then
                Do
                    friday_count +=1
                Loop until StartDate.AddDays(DayToAdd+friday_count)<StartDate.AddDays(friday_count*7)
                Exit for    
            End If
        Next
        newDate = newDate.AddDays(DayToAdd+friday_count)
    
        Return newDate
    
    End Function
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-06
      • 2013-11-18
      • 2017-06-05
      • 2011-10-21
      • 1970-01-01
      • 1970-01-01
      • 2011-10-12
      相关资源
      最近更新 更多