代码之家  ›  专栏  ›  技术社区  ›  Hanz Cheah

给定日期的月发电量

  •  0
  • Hanz Cheah  · 技术社区  · 6 年前

    努力实现

    我在代码上定了一个日期,比如2019年1月31日。然后每天我都会执行我的代码,但只有在2019年2月28日/2020年2月29日、2019年3月31日、2019年4月30日……我想执行代码。有点像一代一代。此外,如果固定日期是2019年1月30日,我希望在2019年2月28日/2020年2月29日、2019年3月30日、2019年4月30日执行代码……

    例如

    我所做的

    我一直在关注这个问题 VBScript DateDiff month ,并已尝试以下代码,但它不起作用。

    如果我有一个日期,比如说2010年1月31日

    endFeb = DateAdd("m",1,"31-Jan-10")
    endMar = DateAdd("m",1,endFeb)
    endApr = DateAdd("m",1,endMar)
    

    结果

    endFeb: 28/02/2010
    endMar: 28/03/2010
    endApr: 28/04/2010
    

    我想要的是

    endFeb: 28/02/2010
    endMar: 31/03/2010
    endApr: 30/04/2010
    

    代码

    sFixedDate = "2019-01-31" '==== Fixed
    sProcessDate = "2019-02-28"  '==== Changes daily
    
    d1 = CDate(sFixedDate)
    d2 = CDate(sProcessDate)
    
    diff = DateDiff("m", d1, d2)
    
    If request("btnProcess") <> "" Then
        If diff Mod 1 = 0 Then  '=== Not as simple as I thought
            '=== Trying to do monthly GENERATION. 
            '===Excecute the CODE
        End If
    End If
    
    4 回复  |  直到 6 年前
        1
  •  3
  •   Ansgar Wiechers    6 年前

    基本上,你想在每个月的最后一天运行一些东西。意思是后一天将是另一个月,所以您可以这样计算下个月的最后一天:

    today    = Date
    tomorrow = today + 1
    If request("btnProcess") <> "" Then
        If Month(today) <> Month(tomorrow) Then
            endNextMonth = DateAdd("m", 1, tomorrow) - 1
        End If
    End If
    

    若要获取任何给定月份的最后一天,请调整要添加到明天日期的月份数。

    上面的假设是你在一个月的最后一天做计算。如果要计算某个月的最后一天,请参见 Ekkehard Horner's answer .

        2
  •  1
  •   Ekkehard.Horner    6 年前

    使用日期序列号:

    For m = 1 To 13
         d1 = DateSerial(2019, m, 1) ' First day of month is easy
         d2 = DateAdd("d", d1, -1)   ' Last day of previous month is just 1 day before
         WScript.Echo m, d1, d2
    Next
    
    cscript lom.vbs
    1 01.01.2019 31.12.2018
    2 01.02.2019 31.01.2019
    3 01.03.2019 28.02.2019
    4 01.04.2019 31.03.2019
    5 01.05.2019 30.04.2019
    6 01.06.2019 31.05.2019
    7 01.07.2019 30.06.2019
    8 01.08.2019 31.07.2019
    9 01.09.2019 31.08.2019
    10 01.10.2019 30.09.2019
    11 01.11.2019 31.10.2019
    12 01.12.2019 30.11.2019
    13 01.01.2020 31.12.2019
    
        3
  •  0
  •   Joe Ka'ainoa    6 年前

    似乎对于给定的开始日期,您希望计算未来x个月的新日期是什么,如果开始日期大于未来月份,则给出该月份的最后一天。

        Function CalculateFutureDate(startDate, monthsInFuture)
            ' Assumes startDate is in the past
            Dim dtRepeatDate
            Dim dtNewDate
            If (IsDate(startDate)) Then
                dtRepeatDate = CDate(startDate)
                ' months between now and Start Date
                Dim intMonthsToAdd
                Dim dtCurrentDate
                dtCurrentDate = Now()
                intMonthsToAdd = DateDiff("m", startDate, dtCurrentDate)
                If intMonthsToAdd > 0 And Day(startDate) < Day(dtCurrentDate) Then
                    intMonthsToAdd = intMonthsToAdd - 1
                End If
                ' Add the future months to the month offset
                intMonthsToAdd = intMonthsToAdd + monthsInFuture
                ' Now calculate future date
                dtNewDate = DateAdd("m", intMonthsToAdd, dtRepeatDate)
                CalculateFutureDate = dtNewDate
            End If
        End Function
    

    然后你可以这样做:

    CalculateFutureDate(CDate("2019-01-31"), intFutureMonths)
    

    这将输出:

    ?CalculateFutureDate(CDate("2019-01-31"), 1)
    2/28/2019
    ?CalculateFutureDate(CDate("2019-01-31"), 2)
    3/31/2019
    ?CalculateFutureDate(CDate("2019-01-31"), 3)
    4/30/2019
    
        4
  •  -1
  •   Hanz Cheah    6 年前
        dtLoan = CDate("2019-01-30")
        dtProcess = CDate ("2020-02-28")
    
        'dtLoan = CDate("2019-01-31")
        'dtProcess = CDate ("2020-02-29")
    
        'dtLoan = CDate("2019-02-28")
        'dtProcess = CDate ("2020-02-29")
    
    
        if LastDateOfMonth(dtLoan) = dtLoan AND dtProcess = LastDateOfMonth(dtProcess) then
    
            response.write " this mean that the Loan date is end of the month, say 31 Jan, 28, 29 of Feb, 31 Feb "
            response.write " and Process Date is also end of the month " & "<br>" 
    
            response.write " **** End of the month Loan Date : " & dtLoan & "<br>"
            response.write " **** End of the month Process Date : " & dtProcess & "<br>"
    
        elseif LastDateOfMonth(dtLoan) <> dtLoan AND dtProcess <> LastDateOfMonth(dtProcess) then
    
            daysFromEndOfLoanMth = DateDiff("d",LastDateOfMonth(dtLoan),dtLoan)
            response.write " How many days from end of Loan month: " & daysFromEndOfLoanMth & "<br>"
    
            daysFromEndOfProcessMth = DateAdd("d",daysFromEndOfLoanMth,LastDateOfMonth(dtProcess))
            response.write " From end of the month Add " & daysFromEndOfLoanMth & " Days = " & daysFromEndOfProcessMth & "<br>"
            response.write " The date of process : " & dtProcess & "<br>"
    
            dtShouldProcess = day(dtLoan) & "/" & Month(dtProcess) & "/" & Year(dtProcess)
    
            if isDate(dtShouldProcess) then
                dtShouldProcess=CDate(dtShouldProcess)
            else
                dtShouldProcess=daysFromEndOfProcessMth
            end if
    
            response.write " ** The date of should Process : ** " & dtShouldProcess & "<br>"
            if dtProcess = dtShouldProcess then
            'if dtProcess = daysFromEndOfProcessMth then
    
                response.write " **** Loan Date : " & dtLoan & "<br>"
                response.write " **** Process Date : " & dtProcess & "<br>"
    
            end if
    
        'daysFromEndOfProcessMth =  DateDiff("d",LastDateOfMonth(dtProcess1),dtProcess1)
        'response.write " How many days from Process Date end of the month: " & daysFromEndOfProcessMth & "<br>"
    
        end if