Ежемесячное поколение с заданными датами - PullRequest
0 голосов
/ 11 января 2019

Пытаясь достичь

Я исправил дату в своем коде, скажем, 31-01-2019. Тогда каждый день я буду выполнять свой код, но только 28-02-2019 / 29-02-2020, 31-03-2019, 30-04-2019 ... Я хочу выполнить код. Это что-то вроде месячного поколения. Кроме того, если фиксированная дата - 30-01-2019, я хочу выполнить код 28-02-2019 / 29-02-2020, 30-03-2019, 30-04-2019 ...

Например

Что я сделал

Я следил за вопросом VBScript DateDiff month и опробовал следующий код, но он не работает.

Если бы у меня была дата, скажем, 31 января 2010 года, DateAdd

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 ]

0 голосов
/ 30 января 2019

Похоже, что для заданной даты начала вы хотите рассчитать 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
0 голосов
/ 15 января 2019

Использовать DateSerial:

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
0 голосов
/ 15 января 2019
    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
0 голосов
/ 11 января 2019

По сути, вы хотите запустить что-то в последний день каждого месяца. Это означает, что послезавтра будет другой месяц, так что вы можете сделать что-то вроде этого для расчета последнего дня следующего месяца:

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

Чтобы получить последний день для любого данного месяца, настройте количество месяцев, добавляемых к завтрашней дате.

Вышеприведенное предполагает, что вы выполняете вычисления в последний день месяца. Если вы хотите рассчитать последний день любого месяца в любой день месяца, см. Ответ Эккехарда Хорнера .

...