Vb script for T-21 days (here T is current date)

130 views Asked by At

Final out put like Today date is 28-Apr-2021

Output like.but it should not include saturdays and sundays(only working days) 27-Apr-2021 26-Apr-2021 23-Apr-2021 Like this...

2

There are 2 answers

0
Étienne Laneville On

Here's a simple way to loop through dates and check the day of the week using Weekday function. This function returns a list of daysToList dates starting from yesterday, skipping weekends:

Function TMinus(daysToList)
    Dim daysFound
    Dim currentDay
    Dim list
    
    ' Initialize variables
    daysFound = 0
    list = ""
    currentDay = Now
    
    Do While daysFound < daysToList
        currentDay = DateAdd("d", -1, currentDay)
        Select Case Weekday(currentDay)
            Case 1, 7
                ' Skip weekends
            Case Else
                list = list & FormatDateTime(currentDay, 2) & vbCrLf
                daysFound = daysFound + 1
        End Select
    Loop
    TMinus = list
End Function
1
Petrus On

I think I have just understood you. What you want is that the output of the file should only be on working days. Well, you can try this:

a = WeekDay("4/26/2021")
 
msgbox a

or this

a = WeekDay(Date)
 
msgbox a

You have to keep in mind that for Windows the day Sunday =1, Monday =2..... Saturday = 7 or you can also define which day of the week you want to be the first day of the week and play with that